在 junit 测试用例中的测试方法之间传递信息

发布于 2024-09-05 18:12:35 字数 485 浏览 4 评论 0原文

目前,我正在为一个工作流程创建一个测试用例,并且工作流程具有不同的步骤。 第一步的结果很重要,因为第二步需要知道步骤 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 技术交流群。

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

发布评论

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

评论(7

春花秋月 2024-09-12 18:12:35

对我来说,这听起来不像单元测试。这很好,但 JUnit 可能不是最好的工具。可以升级到 JUnit 4 吗?
假设是 -

  1. 编写一个 JUnit 运行程序以确保单元测试的顺序
  2. 通过静态变量传递状态。
  3. 使用 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 -

  1. Write a JUnit runner to ensure the order of your unit tests
  2. Pass state via static variables.
  3. Use Assume to validate that step one has worked.
吻安 2024-09-12 18:12:35

您的测试确实应该彼此独立,因此请考虑是否有办法做到这一点。例如,您知道成功的第 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.

旧竹 2024-09-12 18:12:35

传统观点是,最好的解决方案是为每个测试从头开始重新计算所有步骤,通常是在私有帮助器方法中,这样您就不会依赖于 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.

盗梦空间 2024-09-12 18:12:35

看来您正在尝试实现单元测试之外的目标......这完全没问题。尝试使用构建在 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.

此生挚爱伱 2024-09-12 18:12:35

我同意基利安和格雷厄姆的观点。测试应该是独立且独立的。即每个测试应该单独运行,就像它与其他测试一起运行一样。

但如果你有充分的理由支持测试之间的这种依赖关系,也许可以尝试 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.

与君绝 2024-09-12 18:12:35

正如其他人所指出的,您应该使测试彼此独立。实现此目的的一种方法是为每个测试正确设置测试夹具。

但是,您的示例中的测试看起来像是测试同一场景的不同阶段。因此,如果场景不是太复杂,我会考虑将所有内容统一为一个测试方法。

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.

長街聽風 2024-09-12 18:12:35

在这个基本示例中,变量在测试 A 中更改,并且可以在测试 B 中使用

public class BasicTest extends ActivityInstrumentationTestCase2 {
    public BasicTest() throws ClassNotFoundException {
        super(TARGET_PACKAGE_ID, launcherActivityClass);        
    }

    public static class MyClass {    
        public static String myvar = null;              
        public void set(String s) {
            myvar = s;
        }               
        public String get() {
            return myvar;
        }
    }

    private MyClass sharedVar;

    @Override
    protected void setUp() throws Exception {
        sharedVar = new MyClass();
    }

    public void test_A() {
        Log.d(S,"run A");
        sharedVar.set("blah");
    }

    public void test_B() {
        Log.d(S,"run B");       
        Log.i(S,"sharedVar is: " + sharedVar.get());        
    }

}

输出结果为:

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

public class BasicTest extends ActivityInstrumentationTestCase2 {
    public BasicTest() throws ClassNotFoundException {
        super(TARGET_PACKAGE_ID, launcherActivityClass);        
    }

    public static class MyClass {    
        public static String myvar = null;              
        public void set(String s) {
            myvar = s;
        }               
        public String get() {
            return myvar;
        }
    }

    private MyClass sharedVar;

    @Override
    protected void setUp() throws Exception {
        sharedVar = new MyClass();
    }

    public void test_A() {
        Log.d(S,"run A");
        sharedVar.set("blah");
    }

    public void test_B() {
        Log.d(S,"run B");       
        Log.i(S,"sharedVar is: " + sharedVar.get());        
    }

}

output result is:

run A

run B

sharedVar is: blah

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