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
發布數139
最新版本名稱robolectric-4.12.1 (發布於 2024-04-02 11:32:43)
第一版名稱Release0_8 (發布於 )
創建於2010-08-28 00:28:25
推送於2024-05-05 01:50:45
最后一次提交2024-05-04 23:35:34
星數5.8k
關注者數197
派生數1.4k
提交數13.8k
已啟用問題?
問題數2784
打開的問題數423
拉請求數4073
打開的拉請求數114
關閉的拉請求數2017
已啟用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"
}
去到頂部