Command Line Arguments with Visual Studio C # Module Testing

I have to use the testing structure of the VS module to ensure that the code works correctly. However, I have a lot of problems getting tests that require command line arguments (since command line input must be set at runtime ... and with unit tests there is no real "run time"). Is there a way to run my unit tests by entering command line arguments? I realized that this is not an ideal way to create a program, but, unfortunately, I do not decide how the testing process works.

I read that I could potentially write a batch file and include it in the MStest / testcontainer folder. There are some wounds that I need to clear to do it this way, though. These wounds include:

1) I don't know anything about batch files

2) I do not know where the MStest / testcontainer folder is located, how to access it, how it works, or even add files to it (since it seems hidden or inaccessible).

3) I do not know what I would do with the batch file, even if it was written correctly in the MStest / testcontainer folder. How do my tests even suggest knowing him there, not to mention accepting data from him?

So, to summarize: how to do VS unit tests in command line arguments? If I need to use the batch file method, I would really appreciate it being explained to me as if I were 5. I apologize if I am a little helpless in this matter, but I cannot find any clear or useful explanation like any of these things works in this particular context.

Thanks a ton.

+6
source share
2 answers

Short, the wrong approach to unit testing will solve your problem :-):

If your program requires command line arguments, then it must have a static void Main(string[] args) method.

Just write your unit tests to call this method with any command line arguments that you usually add.

Longer, much better: Separate yout functions in 2 - a class that does the job, and just a simple program that takes command line arguments and passes them to the worker.

Testing for write 2 is one thing that Main (xxx) actually passes arguments to your class and many unit tests for your actual worker to test its behavior based on these arguments.

+2
source

In the test method, you can start a new instance of your program with the Process class . Sort of:

 Process prop = new Process(); prop.StartInfo.FileName = "myProgram.exe"; prop.StartInfo.Arguments = "-arg1 -arg2"; prop.StartInfo.RedirectStandardOutput = true; prop.StartInfo.RedirectStandardInput = true; prop.Start(); // You can then use prop.StandardInput and prop.StandardOutput for testing. prop.WaitForExit(); int code = prop.ExitCode; 
0
source

Source: https://habr.com/ru/post/949344/


All Articles