简体   繁体   English

NUnit CustomAttribute 始终将 TestName 显示为 AdHockTestMethod。 我该如何解决?

[英]NUnit CustomAttribute always shows the TestName as AdHockTestMethod. How do I fix this?

I developed a CustomAttribute class for my Test Project called like this:我为我的测试项目开发了一个 CustomAttribute class,如下所示:

[Test, CustomAttribute]

It's job is to get the current Test name upon entry, using this code:它的工作是在输入时获取当前的测试名称,使用以下代码:

var testName = TestContext.CurrentContex.Test.Name

Every time it's called there is at least one instance of this string:每次调用它时,至少有一个该字符串的实例:

AdHockTestMethod AdHock测试方法

I never knew why;我从来不知道为什么; but I put in a filter to ignore it as that is not the Test Name I need.但我输入了一个过滤器来忽略它,因为这不是我需要的测试名称。 (It appears to be an NUnit internals thing, probably based on my use of a BaseClass which requires the use of OneTimeSetUp in a static class to start a reporter). (这似乎是 NUnit 内部的东西,可能基于我使用 BaseClass 需要在 static class 中使用 OneTimeSetUp 来启动记者)。

  • NUnit calls this CustomAttribute multiple times and eventually the Test Name is correct. NUnit 多次调用此 CustomAttribute,最终测试名称是正确的。
  • Up until today, the CustomAttribue code with the AdHockTestMethod filter worked.直到今天,带有AdHockTestMethod过滤器的 CustomAttribue 代码仍然有效。
  • But today the TestName is always AdHocTestMethod但是今天 TestName 始终是AdHocTestMethod
  • I can't seem to find out why this is happening.我似乎无法找出为什么会这样。 Is there something else I'm missing?还有什么我想念的吗?

My Test Code is simply something like this:我的测试代码是这样的:

[TestFixture]
public class TestClassName: BaseClass {
[Test, CustomAttribue]
public async Task TestName(){
}
   // do something asynchronous here
}

NUnit creates an AdhocTestContext when you try to access the test context before one has been created.当您在创建测试上下文之前尝试访问测试上下文时,NUnit 会创建一个 AdhocTestContext。 The name of the (non-existent) test method in the adhoc context is AdHocTestMethod.临时上下文中(不存在的)测试方法的名称是 AdHocTestMethod。 I suspect that's what you are getting.我怀疑这就是你得到的。

The adhoc context is helpful in a particular scenario, which is why we implemented it. adhoc 上下文在特定场景中很有帮助,这就是我们实现它的原因。 However, it's a very rare scenario and it turns out that the adhoc context can be quite confusing in more normal cases.然而,这是一种非常罕见的情况,事实证明,在更正常的情况下,临时上下文可能会相当混乱。

You haven't posted any code to indicate where in your code you are accessing the TestContext but the most common error is to try to access it in a TestCaseSource method, which is not called during the execution of your test but a century or so (in computer time) before.您还没有发布任何代码来指示您在代码中访问 TestContext 的位置,但最常见的错误是尝试在 TestCaseSource 方法中访问它,该方法在您的测试执行期间不会被调用,而是一个世纪左右(在计算机时间)之前。 :-) :-)

Hopefully my guess helps but if not, please edit the question to include code that shows where you are creating and using a TestContext.希望我的猜测有所帮助,但如果没有,请编辑问题以包含显示您在何处创建和使用 TestContext 的代码。

The root cause turned out to be the async method declaration shown above.根本原因原来是上面显示的异步方法声明。 As soon as I removed that part and used Task.Run with Async (inside the method) everything worked.一旦我删除了该部分并将 Task.Run 与 Async (在方法内部)一起使用,一切正常。 The updated code looked like this:更新后的代码如下所示:

[Test, CustomAttribute]
public void TestName(){

  Task.Run(async ()=> await doSomething());
}

The TestContext.CurrentContext.Test.Name returned the proper test name. TestContext.CurrentContext.Test.Name 返回了正确的测试名称。

The CustomAttribute code looked like this: CustomAttribute 代码如下所示:

public class CustomAttribute:Attribue{
  public void CustomAttribute(){
     var TestName = TestContext.CurrentContext.Test.Name;
     if(TestName == "AdhocTestMethod"){
       Nlog.info("Testname was AdhocTestMethod");
       return;
     }
     Start(TestName);
  } 
  private void Start(string TestName){
     Reporter.StartTest(TestName);
  }
}

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

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