简体   繁体   English

使用Nunit映射测试用例ID

[英]Map Testcase ID with Nunit

I'm currently building out my automation framework using Nunit, I've got everything working just fine and the last enchancement I'd like to make is to be able to map my automated test scripts to test cases in my testing software. 我目前正在使用Nunit建立自动化框架,我一切都很好,而我想做的最后一项工作是能够将自动化测试脚本映射到测试软件中的测试用例。

I'm using TestRail for all my testcases. 我所有的测试用例都使用TestRail。

My ideal situation is to be able to decorate each test case with the corresponding testcase ID in test rail and when it comes to report the test result in TestRail, I can just use the Case id. 我的理想情况是能够在测试轨中用相应的测试用例ID装饰每个测试用例,当涉及在TestRail中报告测试结果时,我可以只使用Case ID。 Currently I'm doing this via matching test name/script name. 目前,我正在通过匹配测试名称/脚本名称来执行此操作。

Example - 范例-

[Test]
[TestCaseId("001")]
    public void NavigateToSite()
    {
        LoginPage login = new LoginPage(Driver);
        login.NavigateToLogInPage();
        login.AssertLoginPageLoaded();
    }

And then in my teardown method, it would be something like - 然后在我的拆解方法中,它就像是-

[TearDown]
public static void TestTearDown(IWebDriver Driver)
    {
       var testcaseId = TestContext.CurrentContext.TestCaseid;
       var result = TestContext.CurrentContext.Result.Outcome;

      //Static method to report to Testrail using API
      Report.ReportTestResult(testcaseId, result);
    }

I've just made up the testcaseid attribute, but this is what I'm looking for. 我刚刚组成了testcaseid属性,但这就是我想要的。

[TestCaseId("001")] [TestCaseId( “001”)]

I may have missed this if it already exists, or how do I go about possibly extending Nunit to do this? 如果它已经存在,我可能会错过它,或者我应该如何扩展Nunit来做到这一点?

Any help would be awesome. 任何帮助都是极好的。

Thanks. 谢谢。

You can use PropertyAttribute supplied by NUnit. 您可以使用NUnit提供的PropertyAttribute

Example: 例:

[Property("TestCaseId", "001")]
[Test]
public void NavigateToSite()
{
...
}

[TearDown]
public void TearDown()
{
    var testCaseId = TestContext.CurrentContext.Test.Properties["TestCaseId"];
}

In addition you can create custom property attribute - see NUnit link 此外,您可以创建自定义属性属性-请参见NUnit链接

For many years I recommended that people not do this: mix test management code into the tests themselves. 多年来,我建议人们要这样做:将测试管理代码本身混入测试中。 It's an obvious violation of the single responsibility principle and it creates difficulties in maintaining the tests. 这显然违反了单一责任原则,并且在维护测试方面造成了困难。

In addition, there's the problem that the result presented in TearDown may not be final . 另外,还有一个问题是TearDown中显示的结果可能不是最终的 For example, if you have used [MaxTime] on the test and it exceeds the time specified, your successful test will change to a failure. 例如,如果您在测试中使用了[MaxTime]并且超过了指定的时间,则成功的测试将变为失败。 Several other built-in attributes work this way and of course there is always the possibility of a user-created attribute. 其他一些内置属性也可以通过这种方式工作,当然,用户创建的属性总是存在的。 The purpose of TearDown is to clean up after your code, not as a springboard for creating a reporting or test management system. TearDown的目的是清除代码后的内容,而不是作为创建报告或测试管理系统的跳板。

That said, with older versions of NUnit, folks got into the habit of doing this. 也就是说,使用较旧版本的NUnit,人们开始养成这样做的习惯。 This was in part due to the fact that NUnit addins (the approach we designed) were fairly complicated to write. 部分原因是因为编写NUnit插件(我们设计的方法)相当复杂。 There were also fewer problems because NUNit V2 was significantly less extensible on the test side of things. 由于NUNit V2在测试方面的可扩展性大大降低,因此问题也更少了。 With 3.0, we provided a means for creating test management functions such as this as extensions to the NUnit engine and I suggest you consider using that facility instead of mixing them in with the test code. 在3.0中,我们提供了一种创建测试管理功能的方法,例如对NUnit引擎的扩展,我建议您考虑使用该功能,而不是将其与测试代码混在一起。

The general approach would be to create a property, as suggested in Sulo's answer but to replace your TearDown code with an EventListener extension that reports the result to TestRail. 一般的方法是按照Sulo的答案中的建议创建一个属性,但用EventListener扩展替换您的TearDown代码,该扩展将结果报告给TestRail。 The EventListener has access all the result information - not just the limited fields available in TestContext - in XML format. EventListener以XML格式访问所有结果信息-不仅限于TestContext中可用的有限字段。 You can readily extract whatever needs to go to TestRail. 您可以随时提取需要转到TestRail的所有内容。

Details of writing TestEngine extensions are found here: https://github.com/nunit/docs/wiki/Writing-Engine-Extensions 可在此处找到编写TestEngine扩展的详细信息: https : //github.com/nunit/docs/wiki/Writing-Engine-Extensions

Note that are some outstanding issues if you want to use extensions under the Visual Studio adapter, which we are working on. 请注意,如果您想使用我们正在开发的Visual Studio适配器下的扩展,则是一些未解决的问题。 Right now, you'll get the best experience using the console runner. 现在,您将获得使用控制台运行程序的最佳体验。

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

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