java - 安卓测试套件 : Include all TestCases except some explicitly defined

标签 java android junit

问题:我需要修改 Android Developer TestSuite example 中的代码以便它运行包中的所有测试用例,除了一些明确定义的测试用例。目前它只是运行它们:

public class AllTests extends TestSuite {
    public static Test suite() {
        return new TestSuiteBuilder(AllTests.class)
                .includeAllPackagesUnderHere()
                .build();
    }
}

查看 Docs for TestSuiteBuilder ,也许我可以通过添加对 TestSuiteBuilder 的 addRequirements() 方法的调用来调整上面的代码,但我无法判断这是否会这样做,或者应该用来做这件事。

如果 addRequirements 将而且应该用于排除 AndroidTestCases,我该如何调用它?我不明白我要传递什么论点,文档说:

addRequirements(Predicate...<TestMethod> predicates)
//Exclude tests that fail to satisfy all of the given predicates. 

但我找不到任何关于类 Predicate 的存在或应该如何填充它以实现我的目标的信息。

谢谢

最佳答案

我想在开发期间运行单元测试时排除 InstrumentationTestCases,以便能够在没有功能测试的情况下立即运行测试套件。

这是我的做法:

public class FastTestSuite extends TestSuite {

    public static Test suite() {

        // get the list of all the tests using the default testSuiteBuilder
        TestSuiteBuilder b = new TestSuiteBuilder(FastTestSuite.class);
        b.includePackages("com.your.package.name");
        TestSuite allTest = b.build();


        // select the tests that are NOT subclassing InstrumentationTestCase
        TestSuite selectedTests = new TestSuite();

        for (Test test : Collections.list(allTest.tests())) {

            if (test instanceof TestSuite) {
                TestSuite suite = (TestSuite) test;
                String classname = suite.getName();

                try {
                    Class<?> clazz = Class.forName(classname);
                    if (!InstrumentationTestCase.class.isAssignableFrom(clazz)) {
                        selectedTests.addTest(test);
                    }
                } catch (Exception e) {
                    continue;
                }   
            }   
        }   
        return selectedTests;
    }   
}

关于java - 安卓测试套件 : Include all TestCases except some explicitly defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6041173/

相关文章:

java - 如何在selenium中定位无序列表中的元素

java - 确定文件期限(以天为单位)

android - 使用时间选择器对话框 android 设置倒计时器

java - 最好在 Spring 测试类中使用 @Autowired 或 new?

java - 在 Hudson 中通过 JUnit 运行 Selenium 测试

android - 如何在 android 中使用 JUnit 测试 webService 调用代码?

java - InetAddress.getByName 在代理后面失败

java - jpa查询两个日期之间

java - 我的 Android Studio 应用程序可能出现什么问题?

Android 应用内计费 : If in app purchase is controlled by primary device account then how to prevent passing accounts