Robolectric

Robolectric是一个单元测试框架,可以清除Android SDK jar,以便您可以测试驱动Android应用程序的开发。 (Robolectric is a unit test framework that de-fangs the Android SDK jar so you can test-drive the development of your Android app.)

Github stars Tracking Chart

在Android模拟器或设备上运行测试速度很慢! 构建、部署和启动应用程序通常需要一分钟或更长时间。 没有办法做TDD。 必须有一个更好的方法。

Robolectric是一个单元测试框架,可以清除Android SDK jar,以便您可以测试驱动Android应用程序的开发。 测试在几秒钟内在工作站上的JVM内部运行。

使用Robolectric,您可以编写如下测试:
@RunWith(RobolectricTestRunner.class) public class MyActivityTest {  @Test  public void clickingButton_shouldChangeResultsViewText() throws Exception {    MyActivity activity = Robolectric.setupActivity(MyActivity.class);    Button button = (Button) activity.findViewById(R.id.button);    TextView results = (TextView) activity.findViewById(R.id.results);    button.performClick();    assertThat(results.getText().toString()).isEqualTo("Robolectric Rocks!");  } }

Robolectric通过在加载Android SDK类时加载它们并使其可以在常规JVM上运行成为可能。

Overview

Name With Ownerrobolectric/robolectric
Primary LanguageJava
Program languageJava (Language Count: 5)
Platform
License:Other
Release Count139
Last Release Namerobolectric-4.12.1 (Posted on 2024-04-02 11:32:43)
First Release NameRelease0_8 (Posted on )
Created At2010-08-28 00:28:25
Pushed At2024-04-26 21:41:27
Last Commit At2024-04-26 19:10:12
Stargazers Count5.8k
Watchers Count197
Fork Count1.4k
Commits Count13.7k
Has Issues Enabled
Issues Count2780
Issue Open Count421
Pull Requests Count4063
Pull Requests Open Count114
Pull Requests Close Count2014
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Build Status
GitHub release

Robolectric is the industry-standard unit testing framework for Android. With Robolectric, your tests run in a simulated Android environment inside a JVM, without the overhead of an emulator.

Usage

Here's an example of a simple test written using Robolectric:

@RunWith(AndroidJUnit4.class)
public class MyActivityTest {

  @Test
  public void clickingButton_shouldChangeResultsViewText() throws Exception {
    Activity activity = Robolectric.setupActivity(MyActivity.class);

    Button button = (Button) activity.findViewById(R.id.press_me_button);
    TextView results = (TextView) activity.findViewById(R.id.results_text_view);

    button.performClick();
    assertThat(results.getText().toString(), equalTo("Testing Android Rocks!"));
  }
}

For more information about how to install and use Robolectric on your project, extend its functionality, and join the community of contributors, please visit http://robolectric.org.

Install

Starting a New Project

If you'd like to start a new project with Robolectric tests you can refer to deckard (for either maven or gradle) as a guide to setting up both Android and Robolectric on your machine.

build.gradle:

testImplementation "org.robolectric:robolectric:4.3.1"

Building And Contributing

Robolectric is built using Gradle. Both IntelliJ and Android Studio can import the top-level build.gradle file and will automatically generate their project files from it.

Robolectric supports running tests against multiple Android API levels. The work it must do to support each API level is slightly different, so its shadows are built separately for each. To build shadows for every API version, run:

./gradlew clean assemble install compileTest

Using Snapshots

If you would like to live on the bleeding edge, you can try running against a snapshot build. Keep in mind that snapshots represent the most recent changes on master and may contain bugs.

build.gradle:

repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

dependencies {
    testImplementation "org.robolectric:robolectric:4.4-SNAPSHOT"
}
To the top