Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set command line arguments in unit tests in C#

Is there a possibility to set the command line arguments in unit tests in VS2012? If not, is there an alternative to test many combinations of such arguments besides writing a batch file?

EDIT: I have a console program which reads and parses the passed command line arguments. I would like to assure that the program has the proper behaviour no matter what command line arguments are passed. Instead of trying all combinations over and over again I would like to write a unit test which sets the arguments and starts my program with them.

I don't use any specific test framework. Just the one provided in Visual Studio by creating a new test project.

like image 282
telandor Avatar asked Jun 18 '13 11:06

telandor


People also ask

Is it possible to pass command line arguments to a test?

It is possible to pass custom command line arguments to the test module.

What is command line arguments in C?

What are Command Line Arguments in C? Command line arguments are nothing but simply arguments that are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution.


1 Answers

Why not extract the parsing logic to a separate class and unit test that separately from your main ()?

The parsing class should receive string parameters. Therefore you can test as many scenarios as you need in different tests without actually having to run the program executable, just calling the class instead.

UPDATE:

Now, if you don't want to create an extra class (I would probably still do it, just for clarity, but anyhow), take into account that you CAN just call your static Main(string[] args) method from your unit test, passing different parameters to cover different scenarios.

like image 191
Pablo Romeo Avatar answered Sep 18 '22 22:09

Pablo Romeo