I'm supposed to use the VS unit testing framework to ensure all code is working correctly. However I'm having A LOT of trouble getting tests that require command line arguments to work (since command line inputs must be given at runtime...and with unit tests there is no real "runtime"). Is there a way to run my unit tests with command line argument inputs? I realized this is not the ideal way to construct a program, but I unfortunately do not decide how the testing process works.
I've read that I can potentially write a batch file and include it in the MStest/testcontainer folder. There are some hurtles I have to clear in order to do it this way though. These hurtles include:
1) I know nothing about batch files
2) I don't know where the MStest/testcontainer folder is, how to access it, how it works, or even how to add files to it (since it seems to be hidden or not easily accessible).
3) I don't know what I would do with the batch file even if it was written correctly and in the MStest/testcontainer folder. How are my tests even supposed to know it's there, let alone take input from it?
So to summarize: How does one make VS unit tests take in command line arguments? If I DO have to use the batch file method, I would much appreciate it being explained to me like I'm 5. I apologize if I appear a little helpless in this subject, but I can't find any clear or useful explanations on how any of these things work within this specific context.
Thanks a ton.
To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.
It is possible to pass custom command line arguments to the test module.
To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).
Short, will solve your problem, and incorrect approach to unit testing :-) :
If your program requires command line arguments, then it should have static void Main(string[] args)
method.
Just write your unit tests to call this method with whatever command-line arguments you would normally add.
Longer, much better way: Split yout functionality in 2 - a class which does the work, and just a simple program, which gets the command line arguments and pass them to the worker.
The write 2 unit tests - one, that Main(xxx) actually passes the arguments to your class, and many unit tests for your actual worker to verify its behavior based on these arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With