Experience--junit test framework. doc

Source: Internet
Author: User
Tags assert

1. Same as the test action on Android, but the code is different: Android, to add <applicationandroid:icon= "@drawable/icon" android:label= "in the manifest file String/app_name ">

<uses-libraryandroid:name= "Android.test.runner"/>

Introduce the dependencies of the framework of the Unit test into the project. Note: This piece of code is added to <application>, not <activity>.

....

</application>

<uses-sdkandroid:minsdkversion= "6"/>

<instrumentationandroid:name= "Android.test.InstrumentationTestRunner"

Android:targetpackage= "Cn.itcast.action"//This value must be the same as the current package name. The value of the android:label= "Tests for my App"/>//label property is random, not written.

The way in which the unit test framework was started, he started with several classes

Note: This piece of code is added to the outermost tag

</manifest>; test class also inherits Androidtestcase class

Test methods in Java in the test class in the above to use the annotation @test the word declaration you want to invoke the test class (write this word error ctrl+1 view and then pour the package can be, also can use the ctrl+shift+ oh direct pour)

As with Android, assertions can be set here as well. Give a few of the methods of the assertion class simply: Assert.assertequals (expected,actual); Assert.assertarrayequals (int[]expected,int[] actual); These two methods are comparison methods, comparing expected and actual values. The Assert class has many methods, such as the various array methods above or two object comparisons, two value comparisons, and the True and false: Assert.asserttrue (true); The other method, Assert.assertfalse (False), is the exact opposite;

2. Test a single method and click a single method and run as JUnit Test. To test all the methods, run the test class JUnit test directly.

Another important point: the actual development of the Test class before and after may also add two methods (name can be named): For example, preceded by: public void before () {Put some of the test methods to use the variable, that is, initialize the resource}; After () {This is where you put the code that releases the resources.) These two methods run automatically with the methods of the test method, and the test annotations of the method are added to the beginning of these two methods, such as @beforetest The two methods test that each test method run in the class runtime will invoke the before and after methods.

Another method is to have the public void Beforeclass () {Inside the initialization resource}, followed by the following: the public void Afterclass () {is also the same as the code that releases the resource; The two methods are different from the above. The difference is that the two methods run only once in the execution of the test class, which runs at the time the class is loaded (with the comment @beforeclass) but the first method used in actual development is much more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.