How to call testcase class inside other testcase c

2019-09-01 00:47发布

问题:

I have problem with creating JUnit Test Automation.

My project has many activities (some activities inside other activities). So I create testcase for each activity. But the problem how can i call a testcase inside other testcases (like activity inside other activities).

Can any one give me some idear?

Thanks.....

回答1:

You tests should live in a different project not with your Activities. Then the test runner, usually InstrumentationTestRunner, will be able to discover and run your test cases using instrospection.



回答2:

Disclaimer: this can get very, very messy. If you need one test case to spawn another test case, there's probably a better way of doing it.

JUnit operates on classes. If you want to create tests at runtime, you have to create classes at runtime. Here, the method specializedTester creates an anonymous subclass where getInstance() returns specialized Activity objects for testing.

public abstract class ActivityTestCase extends TestCase {

    public abstract Activity getInstance();

    public static Class specializedTester(final String specialty) {
        return new ActivityTestCase() {
            public Activity getInstance() {
                return new Activity(specialty);
            }
        };
    }

    public void testChildActivities() {
        Activity activity = getInstance();
        for(Activity a : activity.children()) {
            // "check ripeness", "bargain hunt", "check out", etc
            Class c = specializedTester(a.specialty);
            suite.addTestSuite(c);
        }
    }

    static TestSuite suite;

    public static void main(String[] args) {
        suite = new TestSuite(ActivityTestCase.specializedTester("buy groceries"));
        TestRunner.run(suite);
    }
}


标签: android junit