简体   繁体   English

如何正确地将我的测试错误添加到堆栈中,以便测试不会停止执行?

[英]How correctly add my tests' errors to a stack so the tests don't stop executing?

I use SeleniumWebdriver C# to build automated tests. 我使用SeleniumWebdriver C#来构建自动化测试。 I also use NUnit to mark test methods (so I can run them). 我还使用NUnit来标记测试方法(所以我可以运行它们)。

In each of the tests there are several verification and when the first verification fails then the test stops executing (an exception is thrown). 在每个测试中都有几个验证,当第一个验证失败时,测试停止执行(抛出异常)。

I want the test to continue executing so more errors could be found! 我希望测试继续执行,以便找到更多错误!

Please guys, give me a clue how to do it right. 拜托,伙计们,给我一个如何做对的线索。

I think about something like that: 我想到这样的事情:

  1. Instead of throwing Exception in verification I'll add an error to a stack 我没有在验证中抛出异常,而是向堆栈添加错误
  2. When a test ends I check if my stack is not empty 测试结束时,检查我的堆栈是否为空
  3. If the stack is not empty I push to console all errors and fail the test 如果堆栈不为空,我会推送控制所有错误并使测试失败
  4. If the stack is empty then the test passed successfully. 如果堆栈为空,则测试成功通过。

Are these steps good? 这些步骤好吗? Is there a better way? 有没有更好的办法?

I think this should work for you. 我认为这对你有用。

class Asserts
{
    private static StringBuilder _stack;

    [SetUp]
    public void SetUp()
    {
        _stack = new StringBuilder();
    }

    [TearDown]
    public void TearDown()
    {
        if (_stack.Lenght != 0) Assert.Fail(_stack.ToString());
    }

    [Test]
    public void Test()
    {
        AssertHelper(() => Assert.AreEqual(0, 0));
        AssertHelper(() => Assert.IsNotNull(null));
        AssertHelper(() => Assert.AreEqual(3, 4));
        AssertHelper(() => Assert.AreEqual(1, 1));          
    }

    private static void AssertHelper(Action assert)
    {
        try
        {
            assert();
        }
        catch (AssertionException e)
        {
            _stack.Append(e.Message);
        }
    }
}

But the good idea is to keep one verification per one test. 但好的想法是每次测试都要保留一个验证。

Generally you want your tests to only be verifying one thing at a time. 通常,您希望测试只能一次验证一件事。 If there's 2 steps to an action that require asserting, considering having tests like: 如果有一个需要断言的动作有2个步骤,考虑进行如下测试:

UserForm_FirstNameMissing_ThrowsException
UserForm_SecondNameMissing_ThrowsException
UserForm_AgeTooLarge_ThrowsException

instead of having a single test called something like: 而不是像一个单一的测试,如:

UserForm_TestValidation

This means that - like you wanted - if one stage of validation fails, you can continue to test the rest. 这意味着 - 就像你想要的那样 - 如果一个验证阶段失败,你可以继续测试其余部分。 Your best bet for this may be a singular test with a dataset, however keeping the answer simple yet relevant, a test per failure state is good enough for what you want. 对此最好的选择可能是使用数据集的单一测试,但是保持答案简单但相关,每个失败状态的测试对于您想要的内容是足够好的。

I solved my problem in the next way : 我在下一个方面解决了我的问题

  1. I've created a set of methods that are performed once prior to executing any of the tests ([TestFixtureSetUpAttribute] in NUnit); 我创建了一组在执行任何测试之前执行一次的方法(NUnit中的[TestFixtureSetUpAttribute]);
  2. In this set of methods I do the main actions results of which I'll check (in my case I buy a lottery ticket); 在这套方法中,我做了主要的行动结果,我将检查(在我的情况下,我买彩票);
  3. In each test ([Test] in NUnit) I check one assertion. 在每个测试中(NUnit中的[Test])我检查一个断言。

So now I have a main action that doesn't include any verification. 所以现在我有一个不包含任何验证的主要操作。 After this actions is completed I check all needed assertions. 完成此操作后,我会检查所有需要的断言。

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

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