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星跟踪图

在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上运行成为可能。

主要指标

概览
名称与所有者robolectric/robolectric
主编程语言Java
编程语言Java (语言数: 5)
平台
许可证Other
所有者活动
创建于2010-08-28 00:28:25
推送于2025-04-25 18:31:16
最后一次提交2025-04-17 21:57:11
发布数144
最新版本名称robolectric-4.14.1 (发布于 2024-11-21 03:00:38)
第一版名称Release0_8 (发布于 )
用户参与
星数5.9k
关注者数197
派生数1.4k
提交数14.9k
已启用问题?
问题数2852
打开的问题数388
拉请求数4938
打开的拉请求数81
关闭的拉请求数2289
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?

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"
}