我可以使用 Junit 测试作为另一个 Junit 测试的先决条件吗
我有一个 jUnit 测试(我们称之为 T1),其中使用了断言。为了使该断言具有任何价值,我需要验证器来确保该断言是正确的。
对于验证器,我在另一个测试套件中有一个测试(T2)。我能以某种方式让T2成为T1的先决条件吗?
如果不可能的话,如果T1和T2在同一个套房里也可能吗?
I have a jUnit test (lets call it T1) in which I use an assertion. For that assertion to be of any value I need the validator for that assertion to be correct.
For the validator I have a test (T2) in another test Suite. Can I somehow make T2 a precondition for T1.
If that is not possible, would it be possible if T1 and T2 were in the same suite?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当先决条件不发生时,您考虑跳过单元测试。通过编码逻辑跳过单元测试是危险的,因为您不会看到是否存在任何问题,并且您的代码甚至没有获得测试覆盖率。当一个基本部分被破坏时,最好通过更多的测试,而不是隐藏错误。请注意,我们正在谈论单元测试,它应该是独立的并测试软件的小片段。
您可以使用不属于生产代码的标准具验证器,但必须是一个非常简单的测试实用程序,它不会阻止测试的运行。最糟糕的是,您的代码中可能存在另一个未连接到验证器的错误,这些错误可能会延迟出现,只是因为测试代码由于先决条件而跳过了关键代码部分。
如果您想实际进行集成或功能测试,您应该考虑使用 JUnit 之外的其他测试框架。在集成测试中,有时在明确搞砸的开始后跳过其余测试并停止滥用集成环境几分钟/小时是有意义的,但在单元测试中要点是每一段代码都必须得到控制。
当然,您也可以破解 JUnit 中的先决条件,例如在 @BeforeClass/setUp 固定装置中进行断言或将“if”分支放入测试中,但它们会欺骗您。
You think about skipping unit tests when a precondition isn't occur. Skipping unit tests by coded logic is dangerous because you won't see if there is any problem and your code even don't get test coverage. It's better to fail the more tests when a fundamental piece is broken, than to hide errors. Note that we are talking about unit tests, which should be independent and test the small pieces of software.
You can use an etalon validator which isn't part of the production code, but must be a stone simple test utility which doesn't prevent tests to run for sure. The worst thing that there could be another errors in your code, not connected to the validator, which can be delayed to emerge, just because the test code skips the critical code parts due to a precondition.
If you want to do integration or functional testing in real, you should consider using even another test framework than JUnit. In integration testing, sometimes it has sense to skip the rest of the tests and stop abusing the integration environment for minutes/hours after an unequivocally screwed beginning, but in unit testing the main point is that every piece of the code must get control.
Of course you can hack preconditions in JUnit too, for example make an asserion in the @BeforeClass/setUp fixture or putting 'if' forks into the tests, but they will fool you.
您可以评论 T1,并说明它依赖于 T2 才能通过,如果 T2 也失败,则应在解决 T1 之前先修复它
you can comment T1 with the explanation that it relies on T2 to pass and that if T2 also fails it should be fixed first before T1 is tackled