Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] setUp and tearDown functions in tests

I notice that test classes are often structured like so:

    public class FooTest extends BaseTestCase {
        static class FooTestSetup extends TestSetup {
            void setUp() {
                 // ...
            }
            void tearDown() {
                // ...
            }
        }
        public static Test suite() {
            return new FooTestSetup(new TestSuite(FooTest.class));
        }
        void setUp() {
            // ...
        }
        void tearDown() {
            // ...
        }
        // tests
    }

Notice how there are two pairs of setUp/tearDown methods: one inside the test class itself, and one inside a nested class that extends TestSetup.

What this appears to accomplish is that the setUp/tearDown methods inside the TestSetup class run only once, before/after all the tests in the class, while the ones inside the test class itself run before/after each test. Accordingly, the TestSetup methods may do some global setup/teardown such as creating/deleting a project, and the ones inside the test class may do per-test setup/teardown such as opening/closing a particular test file in the editor.

(An example of a test class structured like this is AddIncludeTest.)

I've noticed something unfortunate about this setup. If I run all the tests in the file, by right clicking on the class name and doing Run As -> JUnit Plug-in Test, it calls the setUp/tearDown methods as described above. However, if I run just a single test in the file, by right clicking on the test name and doing Run As -> JUnit Plug-in Test, it does not run the TestSetup setUp/tearDown methods, only the ones in the test class itself; as a result, the test typically fails (for example, because the project that contains the file the test wants to open in the editor has not been created).

Does anyone know a convenient way of running a single test in a test class with this setup?

Also, is the fact that running a single test does not run the TestSetup setUp/tearDown methods perhaps a bug in the testing infrastructure (either on our side, or on JUnit's side)?

Regards,
Nate
 		 	   		  

Back to the top