I am trying to create data provider in C#. However, I don't want to connect the DB. I just want to provide some values. Is there TestNG like unit testing framework in C#, which can I use? Is there any way of doing the following in C# - Microsoft Unit tests (The code was taken from http://www.mkyong.com/unittest/testng-tutorial-6-parameterized-test/)
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(TestNGTest6_3_0 clzz) {
System.out.println("Parameterized Number is : " + clzz.getMsg());
System.out.println("Parameterized Number is : " + clzz.getNumber());
}
//This function will provide the patameter data
@DataProvider(name = "Data-Provider-Function")
public Object[][] parameterIntTestProvider() {
TestNGTest6_3_0 obj = new TestNGTest6_3_0();
obj.setMsg("Hello");
obj.setNumber(123);
return new Object[][]{
{obj}
};
}
回答
The short answer is that yes, you can do this. I don't have the code that will do it to hand, but you will find the basic information at http://msdn.microsoft.com/en-us/library/hh694602.aspx
The Microsoft preferred method is to store the test data in some form of external data source (so that if the data needs to change you don't need to recompile) - the source can be XML, CSV, one of several different database types...
The other method, if your object is mocking something drawn from an actual class in the application in test, is to use the MS fake framework which works in with the unit test framework by allowing you to define fakes and shims to use in place of the real methods (fakes are compile-time, shims are run-time redirects from the real methods - there are other differences as well, but that's the super-short version).
http://msdn.microsoft.com/en-us/library/ms182527.aspx gives more detailed information on creating tests where the data is pulled from an external data source. http://blogs.msdn.com/b/mathew_aniyan/archive/2009/03/17/data-driving-coded-ui-tests.aspx has samples of connection strings for some of the more common data source types.
In addition, the best guide I've seen to using fakes and shims is this Channel9 video: http://channel9.msdn.com/Events/TechEd/Europe/2012/DEV411 (It's a long one, but well worth it)
(I do not work for Microsoft - I just use their tools)
Packages used: