Passing JUnit data between tests

后端 未结 5 1224
鱼传尺愫
鱼传尺愫 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

提交回复
热议问题