繁体   English   中英

如何在 [Setup] 或 [OneTimeSetup] 中传递参数,以便我不必在测试类中调用该方法?

[英]How can I pass parameters in a [Setup] or [OneTimeSetup] so I so not have call the method in my test class?

在 TestNg 中,我们有 @BeforeMethod 可以传递参数。 但是在 Nunit 我得到这个异常“OneTimeSetUp:SetUp 和 TearDown 方法不能有参数:TestInitialize”我试图为类中的每个测试创建范围报告,而不在每个 [Test] 方法中调用 .CreateTest 方法

[SetUp]
 public void TestInitialize(MethodInfo method)
 {
    StartReport(TestContext.CurrentContext.Test.Name);
    string testName = method.Name;
    test = extent.CreateTest(testName);
 }

 [Test]
  public void GetHealthTest()
  {
            test.Log(Status.Info, "Before calling GetHealth API");
            var health = heartbeat.GetHealth();
            test.Log(Status.Info, "After GetHealth API call");
            Assert.AreEqual(health.StatusCode, HttpStatusCode.OK);

  }



如果您只想要SetUp当前测试的名称,请使用TestContext.CurrentContext.Test.Name 还有其他属性,例如FullNameMethodName ,取决于您希望在报告中看到的内容。

也就是说,即使使用SetUp ,这也是一种非常“繁忙”的报告方式。 NUnit 还支持引擎扩展,它允许您在测试程序集本身之外创建报告。 此外,还可以简单地编写一个程序,从测试运行中读取 XML 结果文件并创建报告。

暂无
暂无

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

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