在 junit 测试用例中的测试方法之间传递信息
目前,我正在为一个工作流程创建一个测试用例,并且工作流程具有不同的步骤。 第一步的结果很重要,因为第二步需要知道步骤 1 执行的值。
class TestCaseWorkFlow1 extends TestCase {
private static String resultOfStep1 = null;
public void testStep1(){
if(failed){
resultOfStep1 = "failed";
}
}
public void testStep2(){
if(resultOfStep1 != null ) {
//First test failed.
}else {
}
}
目前
我们使用静态变量在测试方法之间传递信息。
这种情况的最佳解决方案是什么?
请帮忙。
谢谢 J
Currently I am creating a TestCase for one work flow and a workflow have different steps.
The results of one step is important since , second step needs to know value of step1's execution.
class TestCaseWorkFlow1 extends TestCase {
private static String resultOfStep1 = null;
public void testStep1(){
if(failed){
resultOfStep1 = "failed";
}
}
public void testStep2(){
if(resultOfStep1 != null ) {
//First test failed.
}else {
}
}
}
Currently we are using static variable to pass information between testmethods .
What is the best solution for this scenario ?
Please help.
Thanks
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对我来说,这听起来不像单元测试。这很好,但 JUnit 可能不是最好的工具。可以升级到 JUnit 4 吗?
假设是 -
Assume
验证第一步已经起作用了。This does not sound like a unit test to me. That is fine, but JUnit may not be the best tool. Can you upgrade to JUnit 4?
Assuming yes -
Assume
to validate that step one has worked.您的测试确实应该彼此独立,因此请考虑是否有办法做到这一点。例如,您知道成功的第 1 步的结果是什么,因此只需使用模拟对象或其他方法将其作为第 2 步的输入提供即可。或者看看是否有办法打破步骤的顺序耦合,以便每个步骤都可以独立运行。
Your tests should really be independent of each other, so think if there's a way to do that. For example, you know what the outcome of a successful step1 is, so just provide that as input in step2 using a mock object or other approach. Or see if there's a way to break the sequential coupling of the steps so that each can be run independently.
传统观点是,最好的解决方案是为每个测试从头开始重新计算所有步骤,通常是在私有帮助器方法中,这样您就不会依赖于 JUnit 执行测试的顺序(实际上并未定义)按文本顺序)。您可能应该这样做,除非某些步骤需要花费大量时间来重新计算。
The conventional wisdom is that the best solution is to have all the steps recomputed from scratch for each test, usually in a private helper method, so that you don't depend on the order in which JUnit executes your tests (which is not actually defined to be in textual order). You should probably do this, unless some of the steps take a truly horrible amount of time to recompute.
看来您正在尝试实现单元测试之外的目标......这完全没问题。尝试使用构建在 jUnit 之上的其他工具,并为您提供测试行为而不是小代码单元的机制。我已经成功使用了一段时间的工具之一是 jBehave。您最终将使用由与场景步骤匹配的方法执行的纯文本脚本来描述测试场景。
It seems that you are trying to achieve something beyond unit testing... which is perfectly fine. Try using other tools that are built on top of jUnit and provide you with mechanisms for testing behavior rather than small units of code. One of tools that I've been successfully using for some time is jBehave. You'll end up describing test scenarios using plain text scripts executed by methods which match scenario steps.
我同意基利安和格雷厄姆的观点。测试应该是独立且独立的。即每个测试应该单独运行,就像它与其他测试一起运行一样。
但如果你有充分的理由支持测试之间的这种依赖关系,也许可以尝试 TestNG。在 TestNG 中,您可以指定相互依赖的测试。
I agree with Kilian and Graham. Tests should be independent and self-contained. I.e. each test should run alone the same way is it runs with other tests.
But if you have some good reason for such dependency between tests, maybe try TestNG. In TestNG you can specify tests dependent on each other.
正如其他人所指出的,您应该使测试彼此独立。实现此目的的一种方法是为每个测试正确设置测试夹具。
但是,您的示例中的测试看起来像是测试同一场景的不同阶段。因此,如果场景不是太复杂,我会考虑将所有内容统一为一个测试方法。
As others have noted, you should make your tests independent of each other. One way to achieve this is to set up test fixtures correctly for each test.
However, the tests in your example look like they test different phases of the same scenario. So I would consider unifying all into a single test method, if that scenario is not too complicated.
在这个基本示例中,变量在测试 A 中更改,并且可以在测试 B 中使用
输出结果为:
run A
run B
shareVar is: blah
in this basic example, the variable is changed in the test A, and can be used in the test B
output result is:
run A
run B
sharedVar is: blah