简体   繁体   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?

In TestNg We have @BeforeMethod where we can pass parameters.在 TestNg 中,我们有 @BeforeMethod 可以传递参数。 But in Nunit I get this exception "OneTimeSetUp: SetUp and TearDown methods must not have parameters: TestInitialize" I am trying to Create the extent report for each test in the class with out calling .CreateTest method in every [Test] method但是在 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);

  }



If all you want is the name of the current test in your SetUp , use TestContext.CurrentContext.Test.Name .如果您只想要SetUp当前测试的名称,请使用TestContext.CurrentContext.Test.Name There are other properties, like FullName and MethodName as well, depending on what you prefer to see in your report.还有其他属性,例如FullNameMethodName ,取决于您希望在报告中看到的内容。

That said, even with use of SetUp , this is a very "busy" way to do reporting.也就是说,即使使用SetUp ,这也是一种非常“繁忙”的报告方式。 NUnit also supports engine extensions, which allow you to create reports outside of the test assembly itself. NUnit 还支持引擎扩展,它允许您在测试程序集本身之外创建报告。 In addition, it's possible to simply write a program that reads the XML result file from a test run and creates a report.此外,还可以简单地编写一个程序,从测试运行中读取 XML 结果文件并创建报告。

暂无
暂无

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

相关问题 如何在我的方法中传递泛型类,以便此代码可重用 - How can I pass a generic class in my method so that this code becomes reusable 如何将 class 传递给方法,以便该方法可以获取对该 class 的引用并对其采取行动? - How can I pass a class into a method so the method can get a reference to that class and act upon it? 我真的必须以不同的方式编写我的代码,以便可以测试它吗? - Do I really have to do write my code differently so just so I can test it? 如何构造类或函数/方法(或接口),以便作为使用的一部分,可以在{}括号中传递值? - How do I structure a class or a function/method (or interface) so that as part of a using, I can pass values in the {} brackets? 尝试在 main 方法中调用 class 构造函数,以便创建报告卡 - Attempting to call a class constructer in main method so I can create a report card 如何修改此方法以便我可以引用或传递每个循环的数据? - How would I modify this method so I can reference or pass the data of each individual loop? 如何将getter和setter参数传递给连接类? - How can I pass my getters and setters parameters to connection class? 如何在单独的 class 中为方法覆盖或添加属性,以便刷新 WCF 服务不会影响我的更改? - How can I override or add an attribute to a method in a separate class so that refreshing a WCF service doesn't blow away my changes? 如何对“天数列表”进行排序,这样我可以将星期一作为我的第一项而不是星期日? - How to sort my Days List, so I can have my monday as my first Item instead of Sunday? 我怎样才能得到它,这样我就不会让我的角色无限跳跃? - How can i get it so i dont have my character infinitely jump?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM