Android Junit测试和普通的Junit测试

时间:2020-12-25 23:23:46

I'm taking over an android project and I wish to introduce unit tests to the project, to help avoid possible regressions.

我正在接手一个android项目,我希望在这个项目中引入单元测试,以避免可能的回归。

For normal java projects, I have two source folders: src and test. The src source folder contains all of my source files and my test source folder contains all of my unit tests, which I believe is pretty standard for keeping test separate from the source, so you don't have to ship with them.

对于普通java项目,我有两个源文件夹:src和test。src源文件夹包含了我所有的源文件,而我的测试源文件夹包含了我所有的单元测试,我认为这是保持测试与源文件分离的标准,所以您不必附带它们。

I've been doing some reading online and the approach with android apps looks to be a little bit different. Several examples talk about setting up a 2nd project for an android test project and then referencing the android project.

我一直在网上阅读,android应用程序的方法看起来有点不同。几个例子讨论了为android测试项目建立第二个项目,然后引用android项目。

I wish to confirm a few things:

我想确认以下几点:

  1. Is having a 2nd project for the testing the appropriate thing to do when it comes to testing android projects or am I just finding bad examples?
  2. 在测试android项目时,是否有一个测试的第二个项目,或者我只是找到了不好的例子?
  3. Should all unit tests be android unit tests? E.g. Yes they should all be, or no I should mix between android unit tests and junit because junits have less overhead.
  4. 所有的单元测试都应该是android的单元测试吗?例:是的,它们都应该是,或者我不应该在android单元测试和junit之间混用,因为junits开销更小。
  5. What additional benefits do android unit tests give over junit tests? E.g. Handles to the emulator, etc.
  6. 相对于junit测试,android单元测试还有什么额外的好处?例如,模拟器的句柄等。

1 个解决方案

#1


8  

Is having a 2nd project for the testing the appropriate thing to do when it comes to testing android projects or am I just finding bad examples?

在测试android项目时,是否有一个测试的第二个项目,或者我只是找到了不好的例子?

Yes, it's typical to have a separate "test project" for testing Android specific code. http://developer.android.com/tools/testing/testing_android.html

是的,通常有一个单独的“测试项目”来测试Android特定的代码。http://developer.android.com/tools/testing/testing_android.html

Should all unit tests be android unit tests? E.g. Yes they should all be, or no I should mix between android unit tests and junit because junits have less overhead.

所有的单元测试都应该是android的单元测试吗?例:是的,它们都应该是,或者我不应该在android单元测试和junit之间混用,因为junits开销更小。

You'll usually have a mix because you can't test Android specific code on a standard JVM with regular ole' JUnit (not without some helper libraries, more on that in a moment).

您通常会混合使用常规ole' JUnit(不是没有一些助手库,稍后详细介绍),因为您无法在标准JVM上测试Android特定的代码。

In practice I've found that it makes sense to divide your application into plain JVM components and Android portions. For instance, if you need to communicate with a REST API you can have a separate component that does only this and which is plain Java. These types of components can easily be tested with standard JUnit. This type of architecture also leads to a clearer separation of responsibility and often and easier to understand and maintain design as well. (Such components can be included in your Android app as regular JARs.)

在实践中,我发现将应用程序分为普通JVM组件和Android部分是有意义的。例如,如果您需要与REST API进行通信,您可以使用一个单独的组件,该组件只执行此操作,并且是纯Java。可以使用标准的JUnit轻松地测试这些类型的组件。这种类型的体系结构也会导致更清晰的职责分离,并且更容易理解和维护设计。(这些组件可以作为常规jar包含在Android应用程序中。)

What additional benefits do android unit tests give over junit tests? E.g. Handles to the emulator, etc.

相对于junit测试,android单元测试还有什么额外的好处?例如,模拟器的句柄等。

Android testing can be slow and painful because a full Android test runs the Android stack in an emulator (or on a device). Android tests are however necessary for testing the parts of your application that are Android specific, such as Context/Activity/Service and so on.

Android测试缓慢而痛苦,因为一个完整的Android测试在模拟器(或设备上)运行Android堆栈。然而,Android测试对于测试应用程序中特定于Android的部分是必要的,比如上下文/活动/服务等等。

Because of the cumbersome and slow nature of native Android tests several frameworks have been created that mock or stub parts of the SDK and take different approaches to help. You may want to look into Robolectric and Robotium, for instance. (These each have their own pros and cons.)

由于本机Android测试的繁琐和缓慢的特性,已经创建了一些框架,用于模拟或存根部分SDK,并采用不同的方法进行帮助。例如,您可能需要研究Robolectric和Robotium。(它们各有利弊。)

#1


8  

Is having a 2nd project for the testing the appropriate thing to do when it comes to testing android projects or am I just finding bad examples?

在测试android项目时,是否有一个测试的第二个项目,或者我只是找到了不好的例子?

Yes, it's typical to have a separate "test project" for testing Android specific code. http://developer.android.com/tools/testing/testing_android.html

是的,通常有一个单独的“测试项目”来测试Android特定的代码。http://developer.android.com/tools/testing/testing_android.html

Should all unit tests be android unit tests? E.g. Yes they should all be, or no I should mix between android unit tests and junit because junits have less overhead.

所有的单元测试都应该是android的单元测试吗?例:是的,它们都应该是,或者我不应该在android单元测试和junit之间混用,因为junits开销更小。

You'll usually have a mix because you can't test Android specific code on a standard JVM with regular ole' JUnit (not without some helper libraries, more on that in a moment).

您通常会混合使用常规ole' JUnit(不是没有一些助手库,稍后详细介绍),因为您无法在标准JVM上测试Android特定的代码。

In practice I've found that it makes sense to divide your application into plain JVM components and Android portions. For instance, if you need to communicate with a REST API you can have a separate component that does only this and which is plain Java. These types of components can easily be tested with standard JUnit. This type of architecture also leads to a clearer separation of responsibility and often and easier to understand and maintain design as well. (Such components can be included in your Android app as regular JARs.)

在实践中,我发现将应用程序分为普通JVM组件和Android部分是有意义的。例如,如果您需要与REST API进行通信,您可以使用一个单独的组件,该组件只执行此操作,并且是纯Java。可以使用标准的JUnit轻松地测试这些类型的组件。这种类型的体系结构也会导致更清晰的职责分离,并且更容易理解和维护设计。(这些组件可以作为常规jar包含在Android应用程序中。)

What additional benefits do android unit tests give over junit tests? E.g. Handles to the emulator, etc.

相对于junit测试,android单元测试还有什么额外的好处?例如,模拟器的句柄等。

Android testing can be slow and painful because a full Android test runs the Android stack in an emulator (or on a device). Android tests are however necessary for testing the parts of your application that are Android specific, such as Context/Activity/Service and so on.

Android测试缓慢而痛苦,因为一个完整的Android测试在模拟器(或设备上)运行Android堆栈。然而,Android测试对于测试应用程序中特定于Android的部分是必要的,比如上下文/活动/服务等等。

Because of the cumbersome and slow nature of native Android tests several frameworks have been created that mock or stub parts of the SDK and take different approaches to help. You may want to look into Robolectric and Robotium, for instance. (These each have their own pros and cons.)

由于本机Android测试的繁琐和缓慢的特性,已经创建了一些框架,用于模拟或存根部分SDK,并采用不同的方法进行帮助。例如,您可能需要研究Robolectric和Robotium。(它们各有利弊。)