Android翻译_测试最佳实践_测试你的Activity_建立并运行测试用例

本章翻译说明:因为时间精力有限,在这部分完全按照Android 文档翻译,并对容易理解的直接复制,做一定的注释说明,需要翻译做到英汉对照。

本章源地址:http://developer.android.com/training/activity-testing/activity-basic-testing.html

Creating and Running a Test Case

创建并运行测试用例

In order to verify that there are no regressions in the layout design and functional behavior in your application, it's important to create a test for eachActivity in your application. For each test, you need to create the individual parts of a test case, including the test fixture, preconditions test method, and Activitytest methods. You can then run your test to get a test report. If any test method fails, this might indicate a potential defect in your code.

为了保证在你的应用中没有布局视觉上的错误和逻辑上功能性的错误,就需要为每一个Activity创建一个测试。每一个测试里面你都需要创建单独的测试用例,包括测试框架、预先写好的测试方法和Activity的测试方法。你可以运行测试来获得测试报告。如果任何一个方法失败了,那就表明你的代码有潜在的缺陷。

Note: In the Test-Driven Development (TDD) approach, instead of writing most or all of your app code up-front and then running tests later in the development cycle, you would progressively write just enough production code to satisfy your test dependencies, update your test cases to reflect new functional requirements, and iterate repeatedly this way.

注意:在测试驱动开发(TDD)的开发方法中,需要在开发周期中预先定义测试并在开发后执行测试,循序渐进地增加产品代码来满足测试依赖,并根据新的功能需求更新测试用例,而且需要使用此种办法进行反复迭代。

Create a Test Case

创建一个测试用例

Activity tests are written in a structured way. Make sure to put your tests in a separate package, distinct from the code under test.

Activity测试的编写时有组织的。请确保将你的测试放到分开的与被测试代码不同的包中。

By convention, your test package name should follow the same name as the application package, suffixed with".tests". In the test package you created, add the Java class for your test case. By convention, your test case name should also follow the same name as the Java or Android class that you want to test, but suffixed with“Test”.

根据惯例,你的测试包的名字需要和应用包名相同,并添加“.tests”后缀。在你创建的包中,添加Java类作为你的测试用例。根据惯例,你的测试用例的名字也需要和你想要测试的Java或Android类相同,但是要添上"Test"后缀。

To create a new test case in Eclipse:

  1. In the Package Explorer, right-click on the /src directory for your test project and select New > Package.
  2. Set the Name field to <your_app_package_name>.tests (for example,com.example.android.testingfun.tests) and click Finish.
  3. Right-click on the test package you created, and select New > Class.
  4. Set the Name field to <your_app_activity_name>Test (for example, MyFirstTestActivityTest) and clickFinish.

在Eclipse中创建一个新的测试用例:

  1. 在Package Ecplorer标签中,右击/src文件夹,并选择New>Package.
  2. 设置名字为<你的应用的包名>.tests
  3. 右击你刚刚创建的包,选择New>Class
  4. 设置名字为<你的Activity的名字>Test,点击Finish。

 

Set Up Your Test Fixture

建立你自己的测试框架

test fixture consists of objects that must be initialized for running one or more tests. To set up the test fixture, you can override the setUp() and tearDown() methods in your test. The test runner automatically runssetUp() before running any other test methods, and tearDown() at the end of each test method execution. You can use these methods to keep the code for test initialization and clean up separate from the tests methods.

一个测试框架由一个可以实例化的类组成,以确保来运行一个或多个测试。要建立测试框架,你可以在你的测试中复写 serup() 和 tearDown()方法。测试会在运行其他方法之前自动运行 setup(),而tearDown()会在每个测试方法之后执行。你可以使用这些方法初始化测试并使你的代码清楚得和测试方法分开。

To set up your test fixture in Eclipse:

  1. In the Package Explorer, double-click on the test case that you created earlier to bring up the Eclipse Java editor, then modify your test case class to extend one of the sub-classes of ActivityTestCase.

    For example:

    1 public class MyFirstTestActivityTest
    2         extends ActivityInstrumentationTestCase2<MyFirstTestActivity> { 
  2. Next, add the constructor and setUp() methods to your test case, and add variable declarations for theActivity that you want to test.

    For example:

     1 public class MyFirstTestActivityTest
     2         extends ActivityInstrumentationTestCase2<MyFirstTestActivity> {
     3 
     4     private MyFirstTestActivity mFirstTestActivity;
     5     private TextView mFirstTestText;
     6 
     7     public MyFirstTestActivityTest() {
     8         super(MyFirstTestActivity.class);
     9     }
    10 
    11     @Override
    12     protected void setUp() throws Exception {
    13         super.setUp();
    14         mFirstTestActivity = getActivity();
    15         mFirstTestText =
    16                 (TextView) mFirstTestActivity
    17                 .findViewById(R.id.my_first_test_text_view);
    18     }
    19 }

    The constructor is invoked by the test runner to instantiate the test class, while the setUp() method is invoked by the test runner before it runs any tests in the test class.

在Eclipse中建立你的测试框架:

  1. 在Package Ecplorer标签中,双击你先前建立的测试用例打开编辑区,然后修改你的测试用例类继承自ActivityTestCase的一个子类。
    //代码见英文部分
  2. 接下来,在你的测试用例中添加构造方法和setUp()方法,并且为你想要测试的Activity添加变量声明。
    //代码见英文部分
    当setUp()方法在test runner运行测试类里的测试之前被其调用时,构造方法便被test runner调用来实例化一个测试类。

Typically, in the setUp() method, you should:

  • Invoke the superclass constructor for setUp(), which is required by JUnit.
  • Initialize your test fixture state by:
    • Defining the instance variables that store the state of the fixture.
    • Creating and storing a reference to an instance of the Activity under test.
    • Obtaining a reference to any UI components in the Activity that you want to test.

通常,在setUp()方法中,你可以:

  • 调用父类的构造方法,它是被Junit定义的。
  • 以此种约定来初始化你的测试框架:
    • 定义一个实例变量来存储框架的状态值。
    • 创建并储存一个被测试Activity实例的引用。
    • 获取你想要测试的Activity里UI控件的引用

You can use the getActivity() method to get a reference to the Activity under test.

你可以使用getActivity()方法来获取一个被测试Activity的引用。

Add Test Methods to Verify Your Activity

添加测试方法来验证你的Activity

Next, add one or more test methods to verify the layout and functional behavior of your Activity.

接下来,添加一个或者多个测试方法来验证你的Activity的布局或者功能性。

For example, if your Activity includes a TextView, you can add a test method like this to check that it has the correct label text:

举个例子,如果你的Activity包含了一个TextView,你可以添加一个测试方法来检查它的显示的文本是否正确:

1 public void testMyFirstTestTextView_labelText() {
2     final String expected =
3             mFirstTestActivity.getString(R.string.my_first_test);
4     final String actual = mFirstTestText.getText().toString();
5     assertEquals(expected, actual);
6 }

The testMyFirstTestTextView_labelText() method simply checks that the default text of the TextView that is set by the layout is the same as the expected text defined in the strings.xml resource.

testMyFirstTestTextView_labelText()这个方法简单的检查了TextView里在string.xml资源文件里定义的预期的文本值。

Note: When naming test methods, you can use an underscore to separate what is being tested from the specific case being tested. This style makes it easier to see exactly what cases are being tested.

注意:当命名一个函数的时候,你可以使用一个下划线来指明在何种特殊情况下的测试。这种写法可以一目了然地呈现出正在测试的是什么用例。

When doing this type of string value comparison, it’s good practice to read the expected string from your resources, instead of hardcoding the string in your comparison code. This prevents your test from easily breaking whenever the string definitions are modified in the resource file.

当需要通过这种方式进行字符串的比对时,将字符串写到资源文件里是一种很好的实践方式,与此相对的是将字符串硬编码到代码中。如果遇到改动只需要在资源文件里改一下就可以了。

To perform the comparison, pass both the expected and actual strings as arguments to the assertEquals()method. If the values are not the same, the assertion will throw an AssertionFailedError exception.

执行这个比较函数,将预期和实际的字符串作为参数传递给assertEquals()方法。如果两个值不一样,那么断言将会抛出一个AssertionFailedError异常。

If you added a testPreconditions() method, put your test methods after the testPreconditions()definition in your Java class.

如果你加入了一个testPreconditions()方法,在定义测试方法的时候需要在testPreconditions()定义之后进行。

For a complete test case example, take a look at MyFirstTestActivityTest.java in the sample app.

完整的测试用例例子,请查阅样例APP里的MyFirstTestActivityTest.java。

Build and Run Your Test

建立并运行你的测试

 You can build and run your test easily from the Package Explorer in Eclipse.

你可以在Eclipse的Package Explorer中构建并运行你的测试。

To build and run your test:

  1. Connect an Android device to your machine. On the device or emulator, open the Settings menu, selectDeveloper options and make sure that USB debugging is enabled.
  2. In the Project Explorer, right-click on the test class that you created earlier and select Run As > Android Junit Test.
  3. In the Android Device Chooser dialog, select the device that you just connected, then click OK.
  4. In the JUnit view, verify that the test passes with no errors or failures.

建立并运行你的测试:

  1. 连接一个Android设备。在设备后者模拟器上,打开Setting菜单,选择开发者选项并确保USB调试可用。
  2. 在Project Explorer中,右击测试类并选择Run As > Android Junit Test.
  3. 在Android设备选择对话框中,选择刚刚连接的设备,单机OK。
  4. 在JUnit视图中,查看测试通过情况

转载请注明出处,合作翻译请移步:https://github.com/RainFool/AndroidTrain_BestPracticesForTesting

 

posted @ 2015-08-24 20:03  RainFool  阅读(825)  评论(0编辑  收藏  举报