Passing JUnit data between tests

后端 未结 5 1203
鱼传尺愫
鱼传尺愫 2020-12-10 11:07

I just discovered when creating some CRUD tests that you can\'t set data in one test and have it read in another test (data is set back to its initialization between each te

相关标签:
5条回答
  • 2020-12-10 11:26

    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

    0 讨论(0)
  • 2020-12-10 11:34

    JUnit promotes independent tests. One option would be to put the two logical tests into one @Test method.

    TestNG was partly created to allow these kinds of dependencies among tests. It enforces local declarations of test dependencies -- it runs tests in a valid order, and does not run tests that depend on a failed test. See http://testng.org/doc/documentation-main.html#dependent-methods for examples.

    0 讨论(0)
  • 2020-12-10 11:40

    How much processing time do these tests take? If not a lot, then why sweat it. Sure you will create some object unnecessarily, but how much does this cost you?

    @Test
    void testCreateObject() {
        Object obj = unit.createObject();
    }
    
    @Test
    void testReadObject() {
        Object obj = null;
        try {
            obj = unit.createObject(); // this duplicates tests aleady done
        } catch (Exception cause) {
            assumeNoException(cause);
        }
        unit.readObject(obj);
    }
    
    0 讨论(0)
  • 2020-12-10 11:50

    Well, for unit tests your aim should be to test the smallest isolated piece of code, usually method by method. So testCreate() is a test case and testRead() is another. However, there is nothing that stops you from creating a testCreateAndRead() to test the two functions together. But then if the test fails, which code unit does the test fail at? You don't know. Those kind of tests are more like integration test, which should be treated differently.

    If you really want to do it, you can create a static class variable to store the object created by testCreate(), then use it in testRead().

    As I have no idea what version of Junit you talking about, I just pick up the ancient one Junit 3.8:

    Utterly ugly but works:

    public class Test extends TestCase{
    
        static String stuff;
    
        public void testCreate(){
            stuff = "abc";
        }
    
        public void testRead(){
            assertEquals(stuff, "abc");
        }
    }
    
    0 讨论(0)
  • 2020-12-10 11:52

    JUnit is independent test. But, If you have no ways, you can use "static" instance to store it.

    static String storage;
    @Test
    public void method1() {
        storage = "Hello"
    }
    
    @Test
    public void method2() {
        Assert.assertThat(something, is(storage));
    }
    
    0 讨论(0)
提交回复
热议问题