1
votes

I am trying to grab a test result in NUnit 3 upon tear down using the internal ITestResult interface. However when I pass an ITestResult object to the tear down method I get "OneTimeSetup: Invalid signature for SetUp or TearDown method: TestFinished" where TestFinished is listed as my teardown method.

If I don't pass the object the tests work fine. I have tried to move my [TearDown] method to the actual class containing the tests instead of my base class but result in the same error. I would like to have my TestFinish function run upon each test complete so I can act accordingly depending on pass/fail or what is in the exception message rather than using my test try/catch with action structure I have now.

Here is my code structure below:

----A file that starts and ends testing and creates a webdriver object to use---

    [OneTimeSetUp]
    public void Setup()
    {
     //Do some webdriver setup...
    }

----Base Test Class that is used for setup or tear down of testing----

[TestFixture]
public class BaseTestClass
{   
    //Also use the webdriver object created at [OneTimeSetUp]
    protected void TestRunner(Action action)
    {
        //perform the incoming tests.
        try
        {
            action();
        }

        //if the test errors out, log it to the file.
        catch (Exception e)
        {
          //Do some logging...
        }
    }

    [TearDown]
    public void TestFinished(ITestResult i)
    {
        //Handle the result of a test using ITestResult object
    }
}

----Test file that uses the BaseTestClass----

class AccountConfirmation : BaseTestClass
{

    [Test]
    public void VerifyAccountData() {

        TestRunner(() => {
           //Do a test...
        });

    }
}
1

1 Answers

2
votes

Remove the ITestResult from your TearDown method and instead use TestContext.CurrentContext.Result within the method.

For example,

[Test]
public void TestMethod()
{
    Assert.Fail("This test failed");
}

[TearDown]
public void TearDown()
{
    TestContext.WriteLine(TestContext.CurrentContext.Result.Message);
}

Will output,

=> NUnitFixtureSetup.TestClass.TestMethod
This test failed