简体   繁体   English

在C#中的单元测试中设置命令行参数

[英]Set command line arguments in unit tests in C#

Is there a possibility to set the command line arguments in unit tests in VS2012? 是否有可能在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. 只是通过创建一个新的测试项目在Visual Studio中提供的那个。

Why not extract the parsing logic to a separate class and unit test that separately from your main ()? 为什么不将解析逻辑提取到与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. 现在,如果你不想创建一个额外的类(我可能仍然会这样做,只是为了清晰,但无论如何),考虑到你可以从你的单元调用你的静态Main(string[] args)方法测试,传递不同的参数以涵盖不同的场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM