Home > Enterprise >  NUnit CustomAttribute always shows the TestName as AdHockTestMethod. How do I fix this?
NUnit CustomAttribute always shows the TestName as AdHockTestMethod. How do I fix this?

Time:12-03

I developed a CustomAttribute class for my Test Project called like this:

[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

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 calls this CustomAttribute multiple times and eventually the Test Name is correct.
  • Up until today, the CustomAttribue code with the AdHockTestMethod filter worked.
  • But today the TestName is always 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
}

CodePudding user response:

NUnit creates an AdhocTestContext when you try to access the test context before one has been created. The name of the (non-existent) test method in the adhoc context is AdHocTestMethod. I suspect that's what you are getting.

The adhoc context is helpful in a particular scenario, which is why we implemented it. 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. :-)

Hopefully my guess helps but if not, please edit the question to include code that shows where you are creating and using a TestContext.

CodePudding user response:

The CustomAttribute code looked like this:

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

CodePudding user response:

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

  • Related