TestNG like unit testing framework in C# (C sharp)

TestNG like unit testing framework in C# (C sharp)

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)

 

api-testing-csharp

Packages used:

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(67)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-04-16 What is the main difference between a key, an IV and a nonce?
2020-04-16 SymmetricAlgorithm 对称加密算法
2018-04-16 Get-Acl 查看文件权限
2018-04-16 C# 如何调用EventLog
点击右上角即可分享
微信分享提示