简体   繁体   English

如何在Visual Studio 2010中测试函数?

[英]How to test a function in Visual Studio 2010?

I write a small function which accept a string type variable as parameter. 我写了一个小函数,它接受一个字符串类型变量作为参数。

I am wondering if there is any way I can test my function? 我想知道是否有任何方法可以测试我的功能?

I don't want to use Unit Test thing because it is overkill. 我不想使用单元测试的东西,因为它是矫枉过正的。

Thank you for your responses. 谢谢你的回复。

UPDATE UPDATE

Thanks all for your replies. 谢谢大家的回复。

My case is, this function will be trigger by an event handler. 我的情况是,这个函数将由事件处理程序触发。 However, I don't want to bind the handle with my function and I just want to check if that function works. 但是,我不想将句柄与我的函数绑定,我只想检查该函数是否有效。

So the best way is to write a unit test? 那么最好的方法是编写一个单元测试?

You can use the immediate window and type the function call in there to test. 您可以使用immediate window并在那里键入函数调用进行测试。 Typically, one would use this thing while running an application, but it is also available at design time. 通常,人们会在运行应用程序时使用此东西,但它也可以在设计时使用。

Thats what unit tests are for. 那是什么单元测试。 The other option is simply create a new console test to reference it, however simply right clicking on it and creating a new unit test is easy, and the best method - it is not overkill and takes 5 seconds : ) 另一种选择就是创建一个新的控制台测试来引用它,但是只需右键单击它并创建一个新的单元测试很容易,而且最好的方法 - 它不是过度杀伤并需要5秒钟:)

Worst case just delete your test when its done. 最糟糕的情况是在完成后删除您的测试。

You could create a sample application that uses your function and then use the debugger to step through it and verify that everything is working correctly. 您可以创建一个使用您的函数的示例应用程序,然后使用调试器逐步执行它并验证一切正常。

But I have to agree with SLaks - you should write unit tests. 但我必须同意SLaks - 你应该编写单元测试。 In fact, that kind of method sounds like it is begging for some unit testing. 事实上,这种方法听起来像是在讨论一些单元测试。

I would use nunit and create a class library and write unit tests to test the functions. 我会使用nunit并创建一个类库并编写单元测试来测试函数。 A traditional way to quick test it is to use a console app, but this is no longer recommended as unit testing is seen as quick and more resiliant way to test code. 快速测试它的传统方法是使用控制台应用程序,但不再推荐这样做,因为单元测试被视为测试代码的快速且更具弹性的方式。

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

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