从 NUnit 将更多信息添加到 TestResult.xml 文件

发布于 2024-07-04 23:14:06 字数 575 浏览 7 评论 0原文

我希望能够向单元测试添加一条“消息”,以便它实际出现在 NUnit 生成的 TestResult.xml 文件中。 例如,当前生成的是:

<results>
    <test-case name="MyNamespace.Tests.MyTest" executed="True" success="True" time="0.203" asserts="4" />
</results>

我希望能够有一个附加属性(或节点,视情况而定),例如:

<results>
    <test-case name="MyNamespace.Tests.MyTest" executed="True" success="True" time="0.203" asserts="4" message="Tested that some condition was met." />
</results>

这个想法是上面的“消息”将以某种方式在测试方法本身中定义(就我而言,是在运行时生成的)。 我是否缺少某个地方可以做这样的事情?

I would like to be able to add a "message" to a unit test, such that it actually appears within the TestResult.xml file generated by NUnit. For example, this is currently generated:

<results>
    <test-case name="MyNamespace.Tests.MyTest" executed="True" success="True" time="0.203" asserts="4" />
</results>

I would like to be able to have an additional attribute (or node as the case may be), such as:

<results>
    <test-case name="MyNamespace.Tests.MyTest" executed="True" success="True" time="0.203" asserts="4" message="Tested that some condition was met." />
</results>

The idea is that "message" above would somehow be defined within the test method itself (in my case, generated at run-time). Is there a property somewhere that I'm missing to be able to do something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

歌入人心 2024-07-11 23:14:06

您可以使用 TestContext 轻松写出您想要的任何消息。 这是我的设置方式。

我的每个测试都是从测试基类继承的。 这删除了多余的代码。

[TestFixture]
public class TestBase
{

    public IWebDriver driver;

    //[OneTimeSetUp] and [OneTimeTearDown] go here if needed

    [SetUp]
    public void Setup(){
         driver = Shortcuts.SetDriver("my browser");
    }

    [TearDown]
    public void TearDown()
    {
        driver.Quit();
        Comment("@Result: " + TestContext.CurrentContext.Result.Outcome.ToString());
    }

    public void Comment(string _comment)
    {
        TestContext.Out.WriteLine(_comment);
    }
    public void Error(string _error)
    {
        TestContext.Error.WriteLine(_error);
    }

}

您可以看到底部的两个函数在所述 TestContext 中写出任何消息或错误。 这对于可并行测试也能很好地工作。

然后我可以使用该父类来设置我的测试,并写入我的控制台。

//Role Management
public class RoleManagementTests : TestBase
{
    [TestCase]
    public void RoleManagement_7777_1()
    {
        Comment("Expected: User has the ability to view all roles in the system.");
        //Test goes here
    }
}

现在,您可以使用 NUnit Console Runner 在输出 (Visual Studio) 和 TestResult.xml 中查看结果。

You can use the TestContext to easily write out any message you want. Here is how I am setup.

Each of my tests are inherited from a testbase class. This removes redundant code.

[TestFixture]
public class TestBase
{

    public IWebDriver driver;

    //[OneTimeSetUp] and [OneTimeTearDown] go here if needed

    [SetUp]
    public void Setup(){
         driver = Shortcuts.SetDriver("my browser");
    }

    [TearDown]
    public void TearDown()
    {
        driver.Quit();
        Comment("@Result: " + TestContext.CurrentContext.Result.Outcome.ToString());
    }

    public void Comment(string _comment)
    {
        TestContext.Out.WriteLine(_comment);
    }
    public void Error(string _error)
    {
        TestContext.Error.WriteLine(_error);
    }

}

You can see the bottom two functions write out any message or error in said TestContext. This will work nicely with parallizable tests also.

I can then use that parent class to setup my tests, and write to my console.

//Role Management
public class RoleManagementTests : TestBase
{
    [TestCase]
    public void RoleManagement_7777_1()
    {
        Comment("Expected: User has the ability to view all roles in the system.");
        //Test goes here
    }
}

Now you can see the results in the output (Visual Studio) and in the TestResult.xml using NUnit Console Runner.

ぃ双果 2024-07-11 23:14:06

这可能没有抓住要点,但是如何命名测试以便它们表明它们测试的内容 - 那么您甚至可能不需要该消息。

如果事实证明这是绝对必要的,我认为您需要生成自己的测试运行程序,它将(从我的脑海中)读取测试用例的附加属性并将其附加到输出。

This may be missing the point, but how about naming the tests so they indicate what they test - then you may not even need the message.

If it proves to be absolutely necessary, I think you'll need to produce your own testrunner that would (off the top of my head) read an additional attribute off the TestCase and attach it to the output.

ぇ气 2024-07-11 23:14:06

在最近的 NUnit 版本中,您可以执行以下操作:

Assert.AreEqual(250.00, destination.Balance, "some message here");

其中“Some message here”可以是常量消息或在运行时生成并存储在字符串变量中的消息。 但是,如果断言失败,这些消息只会出现在输出中。 但是,通常您只需要有关失败测试的信息,因此我建议通过添加之前的每条消息来构建一个字符串,然后使用该字符串变量作为所有断言中的消息。 这使您可以从失败的测试中获取所需的所有信息。

In the recent NUnit releases you can do:

Assert.AreEqual(250.00, destination.Balance, "some message here");

Where "Some message here" can be a constant message or a message generated at runtime and stored in a string variable. These messages will only appear in the output however if the assertion fails. Usually, however, you only need information about failing tests so I recommend building up a string by adding each previous message and then using that string variable as the message in all of your asserts. This allows you to get all of the information you need from failing tests.

原来是傀儡 2024-07-11 23:14:06

我在运行时看不到任何可用的内容,但您可能需要研究几个功能: 描述 属性和 Property 属性都将文本添加到 XML 输出文件中。 不幸的是,它们都是在编译时定义的。

I can't see anything available at run time, but there are a couple of features that you might want to investigate: the Description attribute and the Property attribute both add text to the XML output file. Unfortunately, they're both defined at compile time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文