Robotium Writing test Cases How to simulate the Beforeclass and Afterclass methods of JUNIT4 1-Conditional judgment method

Source: Internet
Author: User

This article is from: http://blog.csdn.net/zhubaitian/article/details/39293883

Robotium's test class ActivityInstrumentationTestCase2 is a TestCase class that inherits from Junit3, so it does not provide JUNIT4 features. As the online summary says:

1, can not be annotate way to identify the new characteristics of subclasses, such as can not achieve @beforeclass, @afterclass and other characteristics. Only by writing setup and teardown,

2. TestCase can only test case writing with test opening.

So sometimes we don't want to do something repetitive every time we start/complete a case, that is, to achieve JUNIT4 @beforeclass and @afterclass, what should we do?
Take the SDK's own Notepad test case as an example, if now we need to implement two test cases
1, Testaddnotecntittle: Create a Chinese title note

2, Testaddnoteengtitle: Create a note in English title

According to the code provided by the instance, the solo will be initialized in Setup and all open activities will be closed in teardown, i.e. each case will be reinitialized once and all activities are closed:

1 @Override2  Public voidSetUp ()throwsException {3     //setUp () is the run before a test case is started.4     //This is where the solo object is created.5     Super. SetUp ();6      This. Activity = This. getactivity ();7      This. Solo =NewSolo (Getinstrumentation (), getactivity ());8 }9     Ten @Override One  Public voidTearDown ()throwsException { A     //TearDown () is the run after a test case has finished. -     //finishopenedactivities () would finish all the activities that has been opened during the test execution. - solo.finishopenedactivities (); the}

But the fact that we're just going to create two note in this script does not need to initialize the solo and close all activities for each case executed. Google did not find a readily available alternative to @beforeclass and @aferclass.
The following methods of implementation

 Packagecom.example.android.notepad.test;ImportCom.robotium.solo.Solo;ImportAndroid.test.ActivityInstrumentationTestCase2;Importandroid.app.Activity; @SuppressWarnings ("Rawtypes") Public classTccreatenoteextendsactivityinstrumentationtestcase2{Private StaticSolo solo =NULL;  PublicActivity activity; Private Static Final intNumber_total_cases = 2; Private Static intRun = 0; Private StaticClass<?>Launchactivityclass; //corresponds to the two values in the information box generated by the Re-sign.jar    Private StaticString Mainactiviy = Com.example.android.notepad.NotesList ";Private StaticString PackageName = "Com.example.android.notepad"; Static {        Try{Launchactivityclass=Class.forName (Mainactiviy); } Catch(ClassNotFoundException e) {Throw NewRuntimeException (e); }} @SuppressWarnings ("Unchecked")     PublicTccreatenote () {Super(PackageName, Launchactivityclass); } @Override Public voidSetUp ()throwsException {//setUp () is the run before a test case is started. //This is where the solo object is created.        Super. SetUp (); //The variable solo have to is static, since every time after a case ' s finished, this class tccreatenote would is Re-inst Antiated//which would leads to Soto to re-instantiated to be null if it 's not set as static        if(Solo = =NULL) {Tccreatenote.solo=NewSolo (Getinstrumentation (), getactivity ()); }} @Override Public voidTearDown ()throwsException {//Check Whether it ' s the last case executed.Run + =counttestcases (); if(Run >=number_total_cases)        {solo.finishopenedactivities (); }    }     Public voidTestaddnotecntitle ()throwsException {Solo.clickonmenuitem ("Add Note"); Solo.entertext (0, "Chinese label Notes"); Solo.clickonmenuitem ("Save"); Solo.clickinlist (0); Solo.clearedittext (0); Solo.entertext (0, "Text 1"); Solo.clickonmenuitem ("Save"); Solo.assertcurrentactivity ("Expected Noteslist Activity", "Noteslist"); Solo.clicklongontext ("Chinese label Notes"); Solo.clickontext ("Delete"); }         Public voidTestaddnoteengtitle ()throwsException {Solo.clickonmenuitem ("Add Note"); Solo.entertext (0, "English Title Note"); Solo.clickonmenuitem ("Save"); Solo.clickinlist (0); Solo.clearedittext (0); Solo.entertext (0, "Text 1"); Solo.clickonmenuitem ("Save"); Solo.assertcurrentactivity ("Expected Noteslist Activity", "Noteslist"); Solo.clicklongontext ("English Title Note"); Solo.clickontext ("Delete"); }}                        

Robotium Writing test Cases How to simulate JUNIT4 Beforeclass and Afterclass methods 1-Conditional judgment

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.