如何在其他测试用例类中调用测试用例类? [英] How to call testcase class inside other testcase class?

查看:208
本文介绍了如何在其他测试用例类中调用测试用例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建JUnit测试自动化时遇到问题.

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?

谢谢.....

推荐答案

免责声明:这可能会变得非常非常混乱.如果您需要一个测试用例来生成另一个测试用例,则可能有更好的方法.

JUnit对类进行操作.如果要在运行时创建测试,则必须在运行时创建类.在这里,方法specializedTester创建一个匿名子类,其中getInstance()返回专用的Activity对象以进行测试.

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);
    }
}

这篇关于如何在其他测试用例类中调用测试用例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆