【问题标题】:How to read ITestResult with NUnit 3 at test teardown如何在测试拆解时使用 NUnit 3 读取 ITestResult
【发布时间】:2017-05-17 16:31:43
【问题描述】:

我正在尝试在使用内部 ITestResult 接口拆除时获取 NUnit 3 中的测试结果。但是,当我将 ITestResult 对象传递给拆解方法时,我得到“OneTimeSetup:SetUp 或 TearDown 方法的无效签名:TestFinished”,其中 TestFinished 被列为我的拆解方法。

如果我没有通过对象,测试工作正常。我试图将我的 [TearDown] 方法移动到包含测试的实际类而不是我的基类,但会导致相同的错误。我想让我的 TestFinish 函数在每个测试完成时运行,这样我就可以根据通过/失败或异常消息中的内容采取相应的行动,而不是使用我现在拥有的操作结构的测试 try/catch。

下面是我的代码结构:

----一个开始和结束测试并创建一个webdriver对象来使用的文件---

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

----用于设置或拆除测试的基本测试类----

[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
    }
}

----使用BaseTestClass的测试文件----

class AccountConfirmation : BaseTestClass
{

    [Test]
    public void VerifyAccountData() {

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

    }
}

【问题讨论】:

    标签: c# selenium nunit nunit-3.0


    【解决方案1】:

    从您的TearDown 方法中删除ITestResult,并改为在该方法中使用TestContext.CurrentContext.Result

    例如,

    [Test]
    public void TestMethod()
    {
        Assert.Fail("This test failed");
    }
    
    [TearDown]
    public void TearDown()
    {
        TestContext.WriteLine(TestContext.CurrentContext.Result.Message);
    }
    

    会输出,

    => NUnitFixtureSetup.TestClass.TestMethod
    This test failed
    

    【讨论】:

    • 这使用 TestContext.CurrentContext.Result.Outcome 为我指明了正确的方向,非常感谢!这样就可以了:)
    猜你喜欢
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多