简体   繁体   English

Azure Devops Visual Studio 测试任务无法发现具有 TestCaseSource 属性的 Nunit 测试

[英]Azure Devops Visual Studio Tests task cannot discover Nunit tests with the TestCaseSource attribute

I am trying to use the [TestCaseSource] attribute from Nunit with my automated tests run from Azure DevOps.我正在尝试将 Nunit 的 [TestCaseSource] 属性与从 Azure DevOps 运行的自动化测试一起使用。

Example: this test is not discovered示例:未发现此测试

public static object[] TestSource = new[]
{
    new[] { new MyType() },
    new[] { new MyType() },
    new[] { new MyType() }
};
[Test, TestCaseSource("TestSource")]
public void MyTestMethod(MyType value)
{
    ...
}

Unfortunately my test methods cannot be executed because of the " No test assemblies found on the test machine matching the source filter criteria or no tests discovered matching test filter criteria. Verify that test assemblies are present on the machine and test filter criteria is correct. " error.不幸的是,我的测试方法无法执行,因为“在测试机器上找不到与源过滤条件匹配的测试程序集,或者没有发现与测试过滤条件匹配的测试。验证机器上是否存在测试程序集并且测试过滤条件正确。 ”错误。

However when I remove the TestCaseSource attribute from the test methods the tests are able to be executed, meaning they can be found normally, which leads me to the problem that the TestCaseSource attribute is making my tests undiscoverable.但是,当我从测试方法中删除 TestCaseSource 属性时,测试能够被执行,这意味着可以正常找到它们,这导致了 TestCaseSource 属性使我的测试无法发现的问题。

Example: this test is discovered示例:发现此测试

[Test]
public void MyTestMethod()
{
    ...
}

I believe I've setup something wrong in the release and that this should work since the [TestCaseSource] test methods do work fine when I run the tests from Visual Studios Test Explorer.我相信我在版本中设置了一些错误,这应该可以工作,因为当我从 Visual Studios 测试资源管理器运行测试时,[TestCaseSource] 测试方法可以正常工作

My DevOps release pipeline is pretty simple, I use an extract files task with an Archive file patterns like " $(System.DefaultWorkingDirectory)/SolutionName/drop/SolutionName.Tests.zip " and a destination folder of " $(System.DefaultWorkingDirectory)/tests "我的 DevOps 发布管道非常简单,我使用带有存档文件模式的提取文件任务,例如“ $(System.DefaultWorkingDirectory)/SolutionName/drop/SolutionName.Tests.zip ”和“ $(System.DefaultWorkingDirectory)的目标文件夹” /测试

then I have a Visual Studio Test task (2.) that selects test by Test run and search folder of " $(System.DefaultWorkingDirectory)/tests "然后我有一个 Visual Studio 测试任务 (2.),它通过测试运行选择测试并搜索“ $(System.DefaultWorkingDirectory)/tests ”文件夹

My best guess is that maybe I need to set the " Path to custom test adapters " field of the Visual Studio Test task to the NUnit3Adapter Nuget package in my tests project but I am unsure of where/how to get the path to it.我最好的猜测是,也许我需要在我的测试项目中将 Visual Studio 测试任务的“自定义测试适配器路径”字段设置为 NUnit3Adapter Nuget package,但我不确定在哪里/如何获得它的路径。

Do I need to change my pipeline to include the NUnit3Adapter, or is there another reason why my tests are undiscoverable?我是否需要更改我的管道以包含 NUnit3Adapter,或者还有其他原因导致我的测试无法发现? I have searched extensively for a reason and as far as I can find no one else is having this issue with [TestCaseSource] leading me to believe I have done something wrong.我出于某种原因进行了广泛的搜索,据我所知,没有其他人对 [TestCaseSource] 有这个问题,这让我相信我做错了什么。

Update:更新:

After more searching and investigation I think I've found the root of the issue, but I don't know how to fix it.经过更多的搜索和调查,我想我已经找到了问题的根源,但我不知道如何解决它。

When I associate my test methods through visual studio Test Explorer to the Azure DevOps Test cases for associated automation, The name of the test method is not set correctly.当我通过 Visual Studio 测试资源管理器将我的测试方法关联到 Azure DevOps 测试用例以进行关联自动化时,测试方法的名称设置不正确。

Example: given this test method from before示例:从之前给出的这个测试方法

public static object[] TestSource = new[]
{
    new[] { new MyType() },
    new[] { new MyType() },
    new[] { new MyType() }
};
[Test, TestCaseSource("TestSource")]
public void MyTestMethod(MyType value)
{
    ...
}

If I associate the MyTestMethod to a work item, the "Automated test name" field in DevOps will show "SolutionName.Tests.MyTestMethod" as the value.如果我将MyTestMethod与工作项相关联,DevOps 中的“自动测试名称”字段将显示“SolutionName.Tests.MyTestMethod”作为值。

you can see here你可以在这里看到

However I believe this is wrong and instead the name needs to be " SolutionName.Tests.MyTestMethod(MyType) "但是我认为这是错误的,而是名称需要是“ SolutionName.Tests.MyTestMethod(MyType)

I think this because at one point somehow I did get one of my tests associated like "SolutionName.Tests.MyTestMethod(MyType)" and it worked fine.我认为这是因为在某一时刻,我确实得到了一个与“SolutionName.Tests.MyTestMethod(MyType)”相关联的测试,并且效果很好。

My question now is how do I get the "associate test case" function to include the (MyType) at the end of the test method name?我现在的问题是如何让“关联测试用例”function 在测试方法名称的末尾包含(MyType)

It seems I found an answer to my own question and after further trial and error I managed to get everything working.似乎我找到了自己问题的答案,经过进一步的试验和错误,我设法让一切正常工作。

Effectively the end result was, as my update specified the "Associate to test case" function of the Visual Studio Test Explorer did not fill in the "Automated test name" as I would expect.实际上,最终结果是,因为我的更新指定了 Visual Studio 测试资源管理器的“关联到测试用例”function 没有像我预期的那样填写“自动化测试名称”。

Example: without parameters示例:无参数

"SolutionName.Tests.MyTestMethod"

However eventually I figured out you must first execute the test locally .但是最终我发现您必须首先在本地执行测试 Then while the test result has passed, associate to the intended test case.然后,当测试结果通过时,关联到预期的测试用例。

Only after would the parameter part of the test method name show up in the Azure DevOps "Automated test name" field correctly.只有在测试方法名称的参数部分正确显示在 Azure DevOps“自动测试名称”字段中之后。

Example: with parameters示例:带参数

"SolutionName.Tests.MyTestMethod(MyType)"

Once I did this all of my tests were discovered and executed correctly on my agent.一旦我这样做了,我的所有测试都会在我的代理上被发现并正确执行。

I am not sure if this is intentional behavior or not.我不确定这是否是故意行为。 None of my searching returned this as the intended user experience.我的搜索都没有将其作为预期的用户体验返回。 Never the less it worked for me.它对我的工作越少越好。

Note笔记

Although this works, I don't see each individual test result for each iteration from the object[] in azure dev-ops.虽然这可行,但我没有看到 azure 开发操作中的 object[] 的每个迭代的每个单独的测试结果。 Either that is a bug or what I am attempting to do was not intended behavior.要么这是一个错误,要么我试图做的不是预期的行为。 Never the less besides that point everything works for me.除此之外,一切都对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM