C#:尝试添加测试用例名称但收到错误消息

时间:2016-02-10 15:16:20

标签: c# selenium nunit

我正在尝试将测试用例名称添加为文件夹名称。我在Teardown方法中添加它以首先检测是否测试通过或失败,然后相应地在已存在的Pass或Fail文件夹中添加文件夹。

[Test]
public void TestCase12345()
{
   string Name = methodBase.Name;
   teardown(Name);
}
[TearDown]
public void teardown(string testcase)
{
   if (TestContext.CurrentContext.Result.Status == TestStatus.Passed)
   {
   string sourcepath = @"sourcepath";
   string timestamp = DateTime.Now.ToString("yy-MM-dd hh-mm");
   string destpath = (@"destinationlocation" + "Test Case - " + testcase + " " + timestamp);
...
}

错误:

  

SetUp或TearDown方法的签名无效:拆解

我在这里错过了什么?

3 个答案:

答案 0 :(得分:1)

您无法将参数传递给[TearDown]方法,NUnit不支持该方法。例如,要将参数传递给[Test],您可以执行类似这样的操作

[Test]
[TestCase("abc", 123)]
public void TestCase12345(string str, int number)
{
}

但正如我所说,NUnit不支持[TearDown]

作为旁注,检查测试是否成功应该在测试方法中(我发现Assert对此非常有用)。 TearDown仅应用于“清理”,即处置WebDriver以及您为测试创建的任何其他内容,并且在代码完成时不会自动关闭。

  

修改

“然后是什么解决方案。如何添加我称之为创建文件夹的函数名称?”

您可以实施EventListener界面。

  

EventListeners能够响应测试运行过程中发生的事件,通常是通过记录某种信息。

例如

public class TestEventListaener : EventListener
{
    // The test collection started/finished to run. 
    void RunStarted(string name, int testCount);
    void RunFinished(TestResult result );
    void RunFinished(Exception exception );

    void TestStarted(TestName testName)
    {
        // create folder with the test name     
    }
    void TestFinished(TestResult result)
    {
        // if test succeeded insert data to the folder otherwise delete it
    }

    // more `EventListener` methods
}

答案 1 :(得分:0)

除了KjetilNodin的回答之外,我会尝试从拆卸功能中删除测试用例参数,因为它很可能期望没有参数的函数用于TearDown。 如果您需要此功能,我将从函数中删除TearDown属性,并在测试用例的末尾调用它,如示例所示。

答案 2 :(得分:0)

这里有两个不同的问题:

1 =正如其他人所说,NUnit并不允许拆解方法进行争论。

2 =如果从测试中调用方法,则它不能用作TearDown方法,而是作为测试代码的一部分。测试尚未完成,因此没有最终结果。更重要的是,如果您的断言失败,您将永远无法接听电话。

您应该删除参数和调用。使用TestContext获取其中一条注释中建议的测试名称。

相关问题