0

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}
    };
}

1 Answer 1

1

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 here.

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).

This article gives more detailed information on creating tests where the data is pulled from an external data source. This blog 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)

2
  • 1
    In addition to MSTest (Microsoft's unit testing library) another commonly used and more flexible and extensible solution is NUnit. It allows you to use external data sources or reference properties, functions or lists for your data sources.
    – Sam Woods
    Nov 13, 2013 at 21:45
  • It's been a long time since I've used Nunit (4+ years), but it had the option to use external data back then (the project I used it on, we hooked it up to a sqlite db).
    – Nat Bowman
    Aug 9, 2018 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.