如何创建它只能运行指定的测试中的一个或多个类别的Andr​​oid测试套件? [英] How do I create Android test suite which only runs specified tests in one or more classes?

查看:132
本文介绍了如何创建它只能运行指定的测试中的一个或多个类别的Andr​​oid测试套件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以阐明如何组织测试套件的测试,在Android中使用JUnit一些轻?我发现几乎所有的例子是非的工作,我想知道它是什么,我没有收到。

我做了包含了几个测试的Andr​​oidTestCase类和测试套件,其中包括包中的所有测试的一个小例子。这个工程(显然):

包含测试的测试用例类:

 公共类ArithmeticsTest扩展AndroidTestCase {
    SomeClass的sctest;    保护无效设置()抛出异常{
        sctest =新SomeClass的();
        super.setUp();
    }
    / *测试SomeClass.addNumbers(INT,INT)方法:* /
    公共无效testAddNumbers(){
        的assertEquals(9,sctest.addNumbers(3,6));
    }    / *测试SomeClass.addNumbers(INT,INT)方法:* /
    公共无效testSubtractNumbers(){
        的assertEquals(2,sctest.subtractNumbers(6,4));
    }
    保护无效拆解()抛出异常{
        super.tearDown();
    }
}

测试套件类包括在包中的所有测试:

 进口junit.framework.Test;
进口android.test.suitebuilder.TestSuiteBuilder;公共类ProjectTestSuite_AllTests {
    公共静态测试套件(){
        返回新TestSuiteBuilder(ProjectTestSuite_AllTests.class)
            .includeAllPackagesUnderHere()
            。建立();
    }
}

如前所述,这工作。不过,如果我有很多的测试案例,这两个类有多次试验,以及其他类与其他测试(这是有道理的,组织不同类型的测试,IMO)的话,我想创建测试套件,包括针对特定具体的测试类等,使他们能够完全运行。

我已经看到了这样的例子,但我没有得到任何人的工作。有没有人有一个很好的例子?即,一个新的测试套件,其包括从一个或多个测试用例类的特定的测试用例。

更新:

下面的两个例子工作:

运行包中的所有测试:

 公共静态测试套件(){
    返回新TestSuiteBuilder(ProjectTestSuite_AllTests.class)
    .includeAllPackagesUnderHere()
    。建立();
}

运行都在一个特定的类测试的测试(AndroidTestCase类):

 公共静态测试套件(){
    类识别TestClass = ArithmeticsTests.class;
    的TestSuite套件=新的TestSuite(识别TestClass);    返回套房;
}

但是,这并不工作:​​

 公共静态测试套件(){
    的TestSuite套件=新的TestSuite();
    suite.addTest(新ArithmeticsTest(testAddNumbers));
    suite.addTest(新ArithmeticsTest(testSubtractNumbers));    返回套房;
}

最后一个(非工作)的例子是从developer.android.com。这个尝试时,我得到的错误是:


  

构造ArithmeticsTests(字符串)未定义


这是奇怪的,因为我没有试图访问一个构造函数,但在命名测试用例类特定的测试方法(testAddNumbers)。

更新1月11日:

这是一个包含几个测试的测试用例类:

 公共类ArithmeticsTests扩展AndroidTestCase {
    SomeClass的sctest;    保护无效设置()抛出异常{
        sctest =新SomeClass的();
        super.setUp();
    }    公共无效testAddNumbers(){
        的assertEquals(9,sctest.addNumbers(3,6));
    }    公共无效testSubtractNumbers(){
        的assertEquals(2,sctest.subtractNumbers(6,4));
    }    保护无效拆解()抛出异常{
        super.tearDown();
    }
}

和这里的测试套件类中,我尝试包括:(f.ex.)在上面的类中的测试之一:

 公共类ProjectTestSuite_SomeTests {    公共静态测试套件(){
        //这不起作用:
        的TestSuite套件=新的TestSuite();
        suite.addTest(新ArithmeticsTests(testAddNumbers));
        返回套房;
    }
}

该addTest线以上导致这个错误:


  

构造ArithmeticsTests(字符串)未定义


这里是SomeClass的文件我试着在这个例子来测试:

 公共类SomeClass的{    INT classInt;
    串classString;
    项项;    公共SomeClass的(){}    公共SomeClass的(INT词,串CS){
        this.classInt = Cl;
        this.classString = CS;
    }    公众诠释的addNumbers(INT NUM1,NUM2 INT){
        返回NUM1 + NUM2;
    }    公众诠释subtractNumbers(INT NUM1,NUM2 INT){
        返回NUM1,NUM2;
    }
}


解决方案

问题据我所知是AndroidTestCase类只有一个构造函数 - AndroidTestCase()

指定使用字符串的测试用例只能通过TestCase类的JUnit,但不支持AndroidTestCase()。这就是为什么你正面临这个问题。

Can someone shed some light on how to organize tests in test suites, using JUnit in Android? I find almost all examples to be non-working, and I'm wondering what it is that I'm not getting.

I've made a small example with an AndroidTestCase class containing a couple of tests, and a test suite which includes all the tests in the package. This works (apparently):

The test case class containing the tests:

public class ArithmeticsTest extends AndroidTestCase {
    SomeClass sctest;

    protected void setUp () throws Exception {
        sctest = new SomeClass();       
        super.setUp();
    }


    /* Test the SomeClass.addNumbers (int, int) method: */
    public void testAddNumbers () {
        assertEquals(9, sctest.addNumbers(3, 6));
    }

    /* Test the SomeClass.addNumbers (int, int) method: */
    public void testSubtractNumbers () {
        assertEquals(2, sctest.subtractNumbers(6, 4));
    }


    protected void tearDown () throws Exception {
        super.tearDown();
    }
}

The test suite class including all the tests in the package:

import junit.framework.Test;
import android.test.suitebuilder.TestSuiteBuilder;

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

As mentioned, this works. However, if I have many test cases, both classes with several tests, and other classes with other tests (it makes sense to organize different types of tests, imo), then I'd like to create test suites that include specific tests for specific classes etc, so they can be run exclusively.

I've seen examples on this, but I've not gotten anyone to work. Does anyone have a good example? That is, a new test suite which includes specific test cases from one or more of the test case classes.

Update:

The following two examples work:

Run all the tests in the package:

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

Run all the tests in a specific class of tests (AndroidTestCase class):

public static Test suite () {
    Class testClass = ArithmeticsTests.class;
    TestSuite suite = new TestSuite(testClass);

    return suite;
}

However, this does NOT work:

public static Test suite () {
    TestSuite suite= new TestSuite();
    suite.addTest(new ArithmeticsTest("testAddNumbers"));
    suite.addTest(new ArithmeticsTest("testSubtractNumbers"));

    return suite;
}

The last (non-working) example is from developer.android.com. The error I get when trying this is:

The constructor ArithmeticsTests(String) is undefined

Which is strange, since I'm not trying to access a constructor, but name a specific test method in the test case class (testAddNumbers).

UPDATE Jan. 11:

Here's the test case class containing a couple of tests:

public class ArithmeticsTests extends AndroidTestCase {
    SomeClass sctest;

    protected void setUp () throws Exception {
        sctest = new SomeClass();
        super.setUp();
    }

    public void testAddNumbers () {
        assertEquals(9, sctest.addNumbers(3, 6));
    }

    public void testSubtractNumbers () {
        assertEquals(2, sctest.subtractNumbers(6, 4));
    }

    protected void tearDown () throws Exception {
        super.tearDown();
    }
}

And here's the test suite class in which I try to include (f.ex.) ONE of the tests in the above class:

public class ProjectTestSuite_SomeTests {

    public static Test suite () {
        // This does not work:
        TestSuite suite= new TestSuite();
        suite.addTest(new ArithmeticsTests("testAddNumbers"));
        return suite;
    }
}

The addTest line above results in this error:

The constructor ArithmeticsTests(String) is undefined

Here's the SomeClass file I try to test in this example:

public class SomeClass {

    int     classInt;
    String  classString;
    Item    item;

    public SomeClass () {}

    public SomeClass (int ci, String cs) {
        this.classInt    = ci;
        this.classString = cs;
    }

    public int addNumbers (int num1, int num2) {
        return num1+num2;
    }

    public int subtractNumbers (int num1, int num2) {
        return num1-num2;
    }
}

解决方案

The issue AFAIK is that AndroidTestCase class has only one constructor - AndroidTestCase()

Specifying test cases using a string is supported only by TestCase class of JUnit but not AndroidTestCase(). That is why you are facing this issue.

这篇关于如何创建它只能运行指定的测试中的一个或多个类别的Andr​​oid测试套件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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