Unit Test in VS2017

Vs2017中的Live Unit Testing功能非常牛,让你一边写UT,一边就看到UT的执行结果,和被测试类的覆盖率。

具体介绍:http://www.dotnetcurry.com/visualstudio/1363/live-unit-testing-visual-studio-2017


另外面向接口编程,做UT的时候,应该都会用到Mock。项目中使用的Rhino.Mocks

具体介绍:https://spin.atomicobject.com/2017/08/07/intro-mocking-moq/


这里需要注意两个函数:GenerateStub<T> & GenerateMock<T> 

更正:自己试了下,这两个函数都可以用。建议使用GenerateStub,可以为属性赋值。

如果只是做简单实例化一个接口,不需要对该接口函数的输出有任何的要求,那就用GenerateStub。

如果UT的结果需要依据这个接口函数的输出,就要使用GenerateMock。(随后调用Stub)

所以UT代码中,GenerateStub一般调用一次就够了。 GenerateMock可能就需要调用多次。

you should use a mock when you are going to verify that something happened on the object, like a method was called. You should use a stub when you just want the object to be involved in the test to return a value but it is not the thing you are testing. A stub which does not have a expectation fulfilled can never fail a test.


I think the general rule should be that you should only ever have a single mock object in a test, but may have several stubs which provide information to the mock object. I believe that more than 1 mock in a test is a code smell.


How to: Deploy Files for Tests

https://msdn.microsoft.com/en-us/library/ms182475.aspx

猜你喜欢

转载自blog.csdn.net/jfyy/article/details/80667211