命令行参数使用Visual Studio单元测试C# [英] Command Line Arguments with Visual Studio Unit Testing C#

查看:302
本文介绍了命令行参数使用Visual Studio单元测试C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用VS单元测试框架,以确保所有code正常工作。不过我在那些需要命令行参数的工作无法得到测试的人(因为命令行中输入必须在运行时给...和单元测试有没有真正的运行)。有没有一种方法来运行我的单元测试用命令行参数输入?我意识到这不是构建一个程序的理想方式,但遗憾的是我没有决定测试过程中是如何工作的。

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.

我读过,我可能会写一个批处理文件,它包含在MSTEST / testcontainer文件夹中。有一些突飞猛进我为了做虽然这种方式来清除。这些突飞猛进包括:

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)我什么都不知道批处理文件

1) I know nothing about batch files

2)我不知道那里的MSTEST / testcontainer文件夹,如何访问它,它是如何工作的,甚至是如何将文件添加到它(因为它似乎被隐藏或不易到达)。

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)我不知道我会即使它是正确的,并在MSTEST / testcontainer文件夹写批处理文件做的。如何我的测试中,甚至应该知道它的存在,更不用说它需要输入?

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?

总结一下:怎样才能让VS单元测试采取的命令行参数?如果我必须使用批处理文件的方法,我更AP preciate它被解释对我来说,我很5.如果我出现在这个问题有些无奈我道歉,但我找不到任何明确如何任何这些东西这个特定的环境中工作或有用的解释。

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.

由于一吨。

推荐答案

短,将解决你的问题,和不正确的单元测试方法:-):

Short, will solve your problem, and incorrect approach to unit testing :-) :

如果你的程序需要命令行参数,那么它应该有静态无效的主要(字串[] args)方法。

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.

更长,更好的方法:
在2分割YOUT功能 - 一类做的工作,只是一个简单的程序,它得到的命令行参数,并将其传递给工人

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.

写2单元测试 - 一个,那主(XXX)实际上传递参数给你的类,许多单元测试您的实际工作者根据这些参数来验证其行为

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.

这篇关于命令行参数使用Visual Studio单元测试C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆