以编程方式创建测试 [英] Create test programmatically

查看:45
本文介绍了以编程方式创建测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题:我可以在 Visual Studio 中以编程方式创建测试并将它们显示在 Test Explorer 中吗?可行吗?

我正在使用 数据驱动测试 所以我实际上可以做我所有的测试,但它们只显示在测试资源管理器的一个测试中,因为这种方法使用只有一个[TestMethod]

所以有没有办法在逻辑中做一些事情:

foreach(列表中的项目){//创建一个将显示在测试资源管理器中的[TestMethod]"}

foreach" 已经用 数据驱动测试 但也许还有另一种方法可以循环遍历我的数据并为每个数据创建一个测试入口 ?谢谢:)

解决方案

如果您使用 MSTEST 进行测试,则必须编写自定义数据源.然后您的测试将在测试资源管理器中显示为多个测试.

https://docs.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2019>

在 NUnit 的情况下,您可以使用 TestCase 属性此行为

http://nunit.org/docs/2.5/testCase.html

在 XUnit 的情况下,您可以使用 Theory 和 InlineData 来实现此行为

https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test#adding-more-features

Quick question : Can I programmatically create tests in visual studio and have them displayed into the Test Explorer ? is it doable ?

I'm using the logic of Data driven tests so i can actually do all my tests but they are displayed in only one test in the Test Explorer because this method uses only one [TestMethod]

so is there a way to do something in the logic of :

foreach(item in list)
{
     // Create a "[TestMethod]" that will be displayed in the Test Explorer
}

The "foreach" is already done with the Data driven tests but maybe there is another way that allow to cycle through my data and create a Test for each entry ? Thanks :)

解决方案

If you are using MSTEST for testing, you will have to write a custom datasource. Then your test will be displayed as multiple test in test explorer.

https://docs.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2019

In case of NUnit you can use TestCase attribute this behavior

http://nunit.org/docs/2.5/testCase.html

In case of XUnit you can use Theory and InlineData for achieving this behavior

https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test#adding-more-features

这篇关于以编程方式创建测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆