[英]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: 我想到这样的事情:
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 : 我在下一个方面解决了我的问题 :
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.