简体   繁体   English

Android测试套件使用robolectric吗?

[英]Android test suite using robolectric?

I'm trying to search for an example of how to run a tests suite for test classes that use Robolectric, for example I have this class: 我正在尝试搜索有关如何为使用Robolectric的测试类运行测试套件的示例,例如,我有此类:

@Config(sdk = 16, manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class RestaurantModelTest {

    //setUp code here...

    @Test
    public void testFindByLocation() throws Exception {
         //test code here
    }

}

All unit test and assertions of class RestaurantModelTest are passing, but I also have another class lets call it XModelTest (in which all assertions and unit test are passing. RestaurantModelTest所有单元测试和断言都可以通过,但是我还有另一个类可以称之为XModelTest (所有声明和单元测试都可以通过。

My problem 我的问题

I don't find any tutorial/example of how to use a test suite using robolectric. 我找不到有关如何使用robolectric使用测试套件的任何教程/示例。

Should this be done in the same package where my RestaurantModelTest and XModelTest are? 是否应该在我的RestaurantModelTest和XModelTest所在的同一包中完成? If not where? 如果不在哪里?

Also I tried doing this with TestSuite from JUnit but many questions arise should the class with my TestSuite extend extends TestSuite super class? 我也尝试过使用JUnit的TestSuite进行此操作,但是会出现很多问题,我的TestSuite扩展类是否应该扩展TestSuite超级类?

If someone can give me a short example using my RestaurantModelTest and XModelTest classes it would be great. 如果有人可以使用我的RestaurantModelTestXModelTest类给我一个简短的例子,那就太好了。

I believe, I've also partially covered this question answering your second question - Can't run android test suite: Exception in thread “main” 我相信,我也回答了您的第二个问题,部分涵盖了这个问题- 无法运行android测试套件:线程“ main”中的异常

Here's how to write a suite with Robolectric: Let's say we have 2 model classes. 以下是使用Robolectric编写套件的方法:假设我们有2个模型类。

CartModel.java

public class CartModel {
    public float totalAmount;
    public int products;

    public void addToCart(float productPrice) {
        products++;
        totalAmount += productPrice;
    }
}

and RestaurantModel.java RestaurantModel.java

public class RestaurantModel {    
    public int staff;

    public void hire(int numberOfHires) {
        staff += numberOfHires;
    }
}

Let's write some dummy tests for them: 让我们为它们编写一些虚拟测试:

CartModelTest.java

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk=21)
public class CartModelTest {

    @Test
    public void addToCart() throws Exception {
        CartModel cartModel = new CartModel();
        assertEquals(0, cartModel.totalAmount, 0);
        assertEquals(0, cartModel.products);
        cartModel.addToCart(10.2f);
        assertEquals(10.2f, cartModel.totalAmount, 0);
        assertEquals(1, cartModel.products);
    }
}

RestaurantModelTest.java

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk=21)
public class RestaurantModelTest {

    @Test
    public void hire() throws Exception {
        RestaurantModel restaurantModel = new RestaurantModel();
        assertEquals(0, restaurantModel.staff);
        restaurantModel.hire(1);
        assertEquals(1, restaurantModel.staff);
    }
}

And now last step - to group them together into one ModelsTestSuite.java : 现在是最后一步-将它们组合到一个ModelsTestSuite.java

@RunWith(Suite.class)
@Suite.SuiteClasses({
        RestaurantModelTest.class,
        CartModelTest.class
})
public class ModelsTestSuite {}

To run - just right-click in ModelsTestSuite and click "Run ModelsTestSuite". 要运行-只需右键单击ModelsTestSuite ,然后单击“运行ModelsTestSuite”。 That's it! 而已!

NB! NB! In Android Studio 2.0 3b, you have to disable instant run (Preferences -> Build, Execution, Deployment -> Instant Run -> Enable Instant Run - uncheck) in order to run Robolectric tests (to avoid java.lang.RuntimeException: java.lang.ClassNotFoundException: Could not find a class for package: <package name> and class name: com.android.tools.fd.runtime.BootstrapApplication ). 在Android Studio 2.0 3b中,您必须禁用即时运行(首选项->构建,执行,部署->即时运行->启用即时运行-取消选中),以便运行Robolectric测试(以避免java.lang.RuntimeException: java.lang.ClassNotFoundException: Could not find a class for package: <package name> and class name: com.android.tools.fd.runtime.BootstrapApplication )。

I hope, it helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM