Automated tests 如何按特定顺序运行robotium测试?
我有一个robotium测试项目,它有一个测试类。test类包含所有的测试方法。现在,当我运行测试类时,测试方法是按字母顺序运行的。因为我在测试方法之间有一些依赖关系(我知道这不是首选实践),所以我希望测试方法以特定的顺序运行。我拥有依赖关系的原因是,它允许编写更少的代码,并使整个测试类运行得更快。有什么方法可以按特定顺序运行测试方法吗?我让它们按特定顺序运行的方法是为每个测试用例创建一个对象,然后在一个测试用例中实例化每个对象。这允许您确定何时运行什么测试。缺点是只运行一次setup方法 以下是我所做的:Automated tests 如何按特定顺序运行robotium测试?,automated-tests,functional-testing,robotium,Automated Tests,Functional Testing,Robotium,我有一个robotium测试项目,它有一个测试类。test类包含所有的测试方法。现在,当我运行测试类时,测试方法是按字母顺序运行的。因为我在测试方法之间有一些依赖关系(我知道这不是首选实践),所以我希望测试方法以特定的顺序运行。我拥有依赖关系的原因是,它允许编写更少的代码,并使整个测试类运行得更快。有什么方法可以按特定顺序运行测试方法吗?我让它们按特定顺序运行的方法是为每个测试用例创建一个对象,然后在一个测试用例中实例化每个对象。这允许您确定何时运行什么测试。缺点是只运行一次setup方法 以下
...//class variables and such
static {
try {
launcherActivityClass = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public NewRobotiumTest() throws ClassNotFoundException {
super(TARGET_PACKAGE_ID, launcherActivityClass);
}
private Solo solo;
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
to = new TestObject();
/*This resets the test documents.
* If running multiple test methods in this class
* remove this method from the setup and place in first test method*/
to.restTestDocuments();
}
public void testSuite() throws IOException {
try {
// Test the Recommended For You section
RecommendForYou rfy = new RecommendForYou();
rfy.testRecommedForYou(solo, to);
Log.i(TAG, RECOMMENDED + " Section test complete.");
//Test the Music section of the app
Music music = new Music();
music.testMusic(solo, to);
Log.i(TAG, MUSIC + " Section test complete.");
// Test Social Networking section of the app
Social social = new Social();
social.testSocial(solo, to);
Log.i(TAG, SOCIAL + " Section test complete.");
// Test Verizon Apps section of the app
VerizonApps vza = new VerizonApps();
vza.testVerizonApps(solo, to);
Log.i(TAG, VERIZONAPPS + " Section test complete.");
// Test Entertainment section of the app
Entertainment ent = new Entertainment();
ent.testEntertainment(solo, to);
Log.i(TAG, ENTERTAINMENT + " Section test complete.");
// Test Games Section of the app
Games games = new Games();
games.testGames(solo, to);
Log.i(TAG, GAMES + " Section test complete.");
// Test Featured section of the app
Featured featured = new Featured();
featured.testFeatured(solo, to);
Log.i(TAG, FEATURED + " Section test complete.");
// Test Business section of catalog
Business bus = new Business();
bus.testBusiness(solo, to);
Log.i(TAG, BUSINESS + " Section test complete.");
// Test the New Apps link in the ODP catalog
NewApps nat = new NewApps();
nat.testNewApps(solo, to);
Log.i(TAG, NEW + " Section test complete.");
//Test the Top Sellers section of the app
TopSeller ts = new TopSeller();
ts.testTopSellers(solo, to);
Log.i(TAG, TOPSELLER + " Section test complete.");
//Test the Top Download section of the app
TopDownload td = new TopDownload();
td.testTopDownload(solo, to);
Log.i(TAG, TOPDOWNLOAD + " Section test complete.");
//Test the Themes section of the app
Themes themes = new Themes();
themes.testThemes(solo, to);
Log.i(TAG, THEMES + " Section test complete.");
//Test the Tools section of the app
Tools tools = new Tools();
tools.testTools(solo, to);
Log.i(TAG, TOOLS + " Section test complete.");
//Test the News section of the app
News news = new News();
news.testNews(solo, to);
Log.i(TAG, NEWS + " Section test complete.");
//Test the Sports section of the app
Sports sports = new Sports();
sports.testSports(solo, to);
Log.i(TAG, SPORTS + " Section test complete.");
//Test the Reading section of the app
Reading read = new Reading();
read.testReading(solo, to);
Log.i(TAG, READING + " Section test complete.");
//Test the Money section of the app
Money money = new Money();
money.testMoney(solo, to);
Log.i(TAG, MONEY + " Section test complete.");
//Test the Shopping section of the app
Shopping shop = new Shopping();
shop.testShopping(solo, to);
Log.i(TAG, SHOPPING + " Section test complete.");
//Test the Fitness section of the app
Fitness fit = new Fitness();
fit.testFitness(solo, to);
Log.i(TAG, FITNESS + " Section test complete.");
//Test the Travel Section of the app
Travel travel = new Travel();
travel.testTravel(solo, to);
Log.i(TAG, TRAVEL + " Section test complete.");
//Test the Spanish section of the app
Spanish spanish = new Spanish();
spanish.testSpanish(solo, to);
Log.i(TAG, SPANISH + " Section test complete.");
// Test the search functionality
TestSearch test = new TestSearch();
test.testSearchFunctionality(solo, to);
} catch (Exception e) {
e.printStackTrace();
} finally {
printTotals(to);
}
}
@Override
public void tearDown() throws Exception {
try {
bw.close();
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
solo.finishOpenedActivities();
super.tearDown();
}
public void printTotals(TestObject to) throws IOException {
// if there is no SD card
if (Environment.getExternalStorageState() == null) {
directory = new File(Environment.getDataDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(Environment.getDataDirectory()
+ "/Robotium-Screenshots/");
// if no directory exists, create new directory
if (!directory.exists()) {
directory.mkdir();
}
// if phone DOES have sd card
} else if (Environment.getExternalStorageState() != null) {
// search for directory on SD card
directory = new File(Environment.getExternalStorageDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(Environment.getExternalStorageDirectory()
+ "/Robotium-Screenshots/");
// if no directory exists, create new directory to store test
// results
if (!directory.exists()) {
directory.mkdir();
}
}// end of SD card checking
String fileName2 = "TotalTestResults.csv";
File totLogRes = new File(directory, fileName2);
bw = new BufferedWriter(new FileWriter(totLogRes, true));
int totCases = to.getTestCase();
int totPass = to.getTestPass();
int totFail = to.getTestFail();
int totRev = to.getNeedReview();
bw.write("\"\n\"");
bw.write("Total Test Results\",\"" + totCases + "\",\"" + totPass
+ "\",\"" + totFail + "\",\"" + totRev + "\"\n\"");
}
}
我让它们按特定顺序运行的方法是为每个测试用例创建一个对象,然后在一个测试用例中实例化每个对象。这允许您确定何时运行什么测试。缺点是只运行一次setup方法 以下是我所做的:
...//class variables and such
static {
try {
launcherActivityClass = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public NewRobotiumTest() throws ClassNotFoundException {
super(TARGET_PACKAGE_ID, launcherActivityClass);
}
private Solo solo;
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
to = new TestObject();
/*This resets the test documents.
* If running multiple test methods in this class
* remove this method from the setup and place in first test method*/
to.restTestDocuments();
}
public void testSuite() throws IOException {
try {
// Test the Recommended For You section
RecommendForYou rfy = new RecommendForYou();
rfy.testRecommedForYou(solo, to);
Log.i(TAG, RECOMMENDED + " Section test complete.");
//Test the Music section of the app
Music music = new Music();
music.testMusic(solo, to);
Log.i(TAG, MUSIC + " Section test complete.");
// Test Social Networking section of the app
Social social = new Social();
social.testSocial(solo, to);
Log.i(TAG, SOCIAL + " Section test complete.");
// Test Verizon Apps section of the app
VerizonApps vza = new VerizonApps();
vza.testVerizonApps(solo, to);
Log.i(TAG, VERIZONAPPS + " Section test complete.");
// Test Entertainment section of the app
Entertainment ent = new Entertainment();
ent.testEntertainment(solo, to);
Log.i(TAG, ENTERTAINMENT + " Section test complete.");
// Test Games Section of the app
Games games = new Games();
games.testGames(solo, to);
Log.i(TAG, GAMES + " Section test complete.");
// Test Featured section of the app
Featured featured = new Featured();
featured.testFeatured(solo, to);
Log.i(TAG, FEATURED + " Section test complete.");
// Test Business section of catalog
Business bus = new Business();
bus.testBusiness(solo, to);
Log.i(TAG, BUSINESS + " Section test complete.");
// Test the New Apps link in the ODP catalog
NewApps nat = new NewApps();
nat.testNewApps(solo, to);
Log.i(TAG, NEW + " Section test complete.");
//Test the Top Sellers section of the app
TopSeller ts = new TopSeller();
ts.testTopSellers(solo, to);
Log.i(TAG, TOPSELLER + " Section test complete.");
//Test the Top Download section of the app
TopDownload td = new TopDownload();
td.testTopDownload(solo, to);
Log.i(TAG, TOPDOWNLOAD + " Section test complete.");
//Test the Themes section of the app
Themes themes = new Themes();
themes.testThemes(solo, to);
Log.i(TAG, THEMES + " Section test complete.");
//Test the Tools section of the app
Tools tools = new Tools();
tools.testTools(solo, to);
Log.i(TAG, TOOLS + " Section test complete.");
//Test the News section of the app
News news = new News();
news.testNews(solo, to);
Log.i(TAG, NEWS + " Section test complete.");
//Test the Sports section of the app
Sports sports = new Sports();
sports.testSports(solo, to);
Log.i(TAG, SPORTS + " Section test complete.");
//Test the Reading section of the app
Reading read = new Reading();
read.testReading(solo, to);
Log.i(TAG, READING + " Section test complete.");
//Test the Money section of the app
Money money = new Money();
money.testMoney(solo, to);
Log.i(TAG, MONEY + " Section test complete.");
//Test the Shopping section of the app
Shopping shop = new Shopping();
shop.testShopping(solo, to);
Log.i(TAG, SHOPPING + " Section test complete.");
//Test the Fitness section of the app
Fitness fit = new Fitness();
fit.testFitness(solo, to);
Log.i(TAG, FITNESS + " Section test complete.");
//Test the Travel Section of the app
Travel travel = new Travel();
travel.testTravel(solo, to);
Log.i(TAG, TRAVEL + " Section test complete.");
//Test the Spanish section of the app
Spanish spanish = new Spanish();
spanish.testSpanish(solo, to);
Log.i(TAG, SPANISH + " Section test complete.");
// Test the search functionality
TestSearch test = new TestSearch();
test.testSearchFunctionality(solo, to);
} catch (Exception e) {
e.printStackTrace();
} finally {
printTotals(to);
}
}
@Override
public void tearDown() throws Exception {
try {
bw.close();
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
solo.finishOpenedActivities();
super.tearDown();
}
public void printTotals(TestObject to) throws IOException {
// if there is no SD card
if (Environment.getExternalStorageState() == null) {
directory = new File(Environment.getDataDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(Environment.getDataDirectory()
+ "/Robotium-Screenshots/");
// if no directory exists, create new directory
if (!directory.exists()) {
directory.mkdir();
}
// if phone DOES have sd card
} else if (Environment.getExternalStorageState() != null) {
// search for directory on SD card
directory = new File(Environment.getExternalStorageDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(Environment.getExternalStorageDirectory()
+ "/Robotium-Screenshots/");
// if no directory exists, create new directory to store test
// results
if (!directory.exists()) {
directory.mkdir();
}
}// end of SD card checking
String fileName2 = "TotalTestResults.csv";
File totLogRes = new File(directory, fileName2);
bw = new BufferedWriter(new FileWriter(totLogRes, true));
int totCases = to.getTestCase();
int totPass = to.getTestPass();
int totFail = to.getTestFail();
int totRev = to.getNeedReview();
bw.write("\"\n\"");
bw.write("Total Test Results\",\"" + totCases + "\",\"" + totPass
+ "\",\"" + totFail + "\",\"" + totRev + "\"\n\"");
}
}
您可以尝试按照JUnit建议组织测试,如下所述:
您可以尝试按照JUnit建议组织测试,如下所述:
创建一个包含testsuit顺序的类。例如:
package com.android.test;
import junit.framework.TestSuite;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
public class AllTests extends ActivityInstrumentationTestCase2<Activity> {
public AllTests(Class<Activity> activityClass) {
super(activityClass);
}
public static TestSuite suite() {
TestSuite t = new TestSuite();
t.addTestSuite(SplashScreenTest.class);
t.addTestSuite(MainLoginScreenTest.class);
t.addTestSuite(EmailSignUpScreenTest.class);
t.addTestSuite(EmailVerificationTest.class);
t.addTestSuite(EmailLoginScreenTest.class);
return t;
}
@Override
public void setUp() throws Exception {
}
@Override
public void tearDown() throws Exception {
}
}
package com.android.test;
导入junit.framework.TestSuite;
导入android.app.Activity;
导入android.test.ActivityInstrumentationTestCase2;
公共类AllTests扩展ActivityInstrumentationTestCase2创建一个包含testsuit顺序的类。例如:
package com.android.test;
import junit.framework.TestSuite;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
public class AllTests extends ActivityInstrumentationTestCase2<Activity> {
public AllTests(Class<Activity> activityClass) {
super(activityClass);
}
public static TestSuite suite() {
TestSuite t = new TestSuite();
t.addTestSuite(SplashScreenTest.class);
t.addTestSuite(MainLoginScreenTest.class);
t.addTestSuite(EmailSignUpScreenTest.class);
t.addTestSuite(EmailVerificationTest.class);
t.addTestSuite(EmailLoginScreenTest.class);
return t;
}
@Override
public void setUp() throws Exception {
}
@Override
public void tearDown() throws Exception {
}
}
package com.android.test;
导入junit.framework.TestSuite;
导入android.app.Activity;
导入android.test.ActivityInstrumentationTestCase2;
公共类所有测试扩展ActivityInstrumentationTestCase2