I have generated a robotium test case and successfully tested the same on real device. Now, i like to know how can i run the same test case (Class that extends ActivityInstrumentationTestCase2) repeatedly.
Please let me know if any code sample for doing the same.
Thanks
I achieved this 2 ways ..
Method 1 - Same Test case can be called many times from a class that extends InstrumentationTestRunner.
import junit.framework.TestSuite;
import android.test.InstrumentationTestRunner;
import android.test.InstrumentationTestSuite;
public class MediaFrameworkTestRunner extends InstrumentationTestRunner {
#Override
public TestSuite getAllTests() {
InstrumentationTestSuite suite = new InstrumentationTestSuite(this);
suite.addTestSuite(MainActivityTest.class);
suite.addTestSuite(MainActivityTest.class);
return suite;
}
#Override
public ClassLoader getLoader() {
return MediaFrameworkTestRunner.class.getClassLoader();
}
}
Method 2 - Add a loop statement inside the testRecorded() method
Related
I am developing a test for an Android app using cucumber-jvm. I wrote a feature and the corresponding steps. The console says that there is any test.
Did you have already this problem?
I don't know exactly what I'm doing wrong.
Running tests Test running startedTest running failed: No test results
Empty test suite.
"StepsDefinitions.java"
#CucumberOptions(features = "features")
public class StepsDefinitions extends ActivityInstrumentationTestCase2<LoginActivity> {
public StepsDefinitions() {
super(LoginActivity.class);
assertNotNull(getActivity());
Log.i("That","It is running.");
}
#Given("^I have a UserBox$")
public void I_am_on_the_Login_Screen() {
EditText etLoginUser = (EditText) getActivity().findViewById(R.id.User);
assertNotNull(etLoginUser);
}
#Then("^I should see on the display$")
public void I_should_see_s_on_the_display() {
EditText display = (EditText) getActivity().findViewById(R.id.Pass);
} }
Do you have the Runner class? You just need a small 'marker' class to tell JUnit to invoke the Cucumber test suite (put it in the same package as your StepDefinitions).
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
public class RunCukesTest {
// No further code needed
}
I've been struggling for a while with this. When creating a test suite in JUnit/Android, I can to the following:
Add all the tests (in all the classes) that exist in the same package as the suite
Add a specific class contatining testMethods
However, I'm completely unable to do the following:
Add a specific testMethod from a specific class to the test suite.
Now, I understand that this SHOULD be possible, as there are countless examples showing this.
This is how it's supposed to work:
The test class contatining the test methods:
import com.frank.android.lookup.SomeClass;
import android.test.AndroidTestCase;
public class ArithmeticsTests extends AndroidTestCase {
SomeClass sctest;
protected void setUp () throws Exception {
sctest = new SomeClass();
super.setUp();
}
public void testAddNumbers () {
assertEquals(9, sctest.addNumbers(3, 6));
}
public void testSubtractNumbers () {
assertEquals(2, sctest.subtractNumbers(6, 4));
}
protected void tearDown () throws Exception {
super.tearDown();
}
}
And here's the test suite class:
import junit.framework.TestSuite;
public class ProjectTestSuite_SomeTests extends TestSuite {
public static Test suite () {
TestSuite suite = new TestSuite("ArithmeticsTests");
suite.addTest(new ArithmeticsTests("testAddNumbers"));
suite.addTest(new ArithmeticsTests2("testSubtractNumbers"));
return suite;
}
}
Now, the two lines where I try adding the individual test methods result in this error:
The constructor ArithmeticsTests(String) is undefined
Now, I've looke around for a long time, and I cannot find any explanation for this. It seems that something is missing, since it doesn't understand what I'm trying to do. The "string" it complains about is in fact the name of the method - I'm not trying to pass a string to a constructor of the class - I'm trying to add the method of the class to the test suite.
I'm using the JUnit version that's included with the Android SDK here, and I haven't installed anything else related to that. Is there something missing? (Obviously there is, bit what?)
EDIT:
I added a construtor to the ArithmeticsTests class:
public ArithmeticsTests (String s) {}
Now the above error is gone.
However, when I run the test suite, I get this error:
testSuiteCreationFailed
....
Caused by: java.lang.NullPointerException: Method name must not be null.
I came up with the same problem and discovered that, while AndroidTestCase does not have a constructor taking a String parameter, it does have a setName(String name) method. By calling the setName method, you can add individual method to the test case.
Using the code in your example, your test suite may look like:
import junit.framework.TestSuite;
public class ProjectTestSuite_SomeTests extends TestSuite {
public static Test suite () {
TestSuite suite = new TestSuite("ArithmeticsTests");
ArithmeticsTests arithmeticsTests = new ArithmeticsTests();
arithmeticsTests.setName("testAddNumbers");
suite.addTest(arithmeticsTests);
ArithmeticsTests2 arithmeticsTest2 = new ArithmeticsTests2();
arithmeticsTest2.setName("testSubtractNumbers");
suite.addTest(arithmeticsTest2);
return suite;
}
}
This worked for me.
public static TestSuite suite() {
final TestSuite t = new TestSuite();
t.addTest(TestSuite.createTest(TestExampleClass1.class, "test1"));
t.addTest(TestSuite.createTest(TestExampleClass2.class, "test2"));
t.addTest(TestSuite.createTest(TestExampleClass3.class, "test2"));
return t;
}
I have problem with creating JUnit Test Automation.
My project has many activities (some activities inside other activities).
So I create testcase for each activity. But the problem how can i call a testcase inside other testcases (like activity inside other activities).
Can any one give me some idear?
Thanks.....
You tests should live in a different project not with your Activities.
Then the test runner, usually InstrumentationTestRunner, will be able to discover and run your test cases using instrospection.
Disclaimer: this can get very, very messy. If you need one test case to spawn another test case, there's probably a better way of doing it.
JUnit operates on classes. If you want to create tests at runtime, you have to create classes at runtime. Here, the method specializedTester creates an anonymous subclass where getInstance() returns specialized Activity objects for testing.
public abstract class ActivityTestCase extends TestCase {
public abstract Activity getInstance();
public static Class specializedTester(final String specialty) {
return new ActivityTestCase() {
public Activity getInstance() {
return new Activity(specialty);
}
};
}
public void testChildActivities() {
Activity activity = getInstance();
for(Activity a : activity.children()) {
// "check ripeness", "bargain hunt", "check out", etc
Class c = specializedTester(a.specialty);
suite.addTestSuite(c);
}
}
static TestSuite suite;
public static void main(String[] args) {
suite = new TestSuite(ActivityTestCase.specializedTester("buy groceries"));
TestRunner.run(suite);
}
}
I've got the basic scenario: a test project, in it - one test class, nothing less, nothing more. The code of the test class is this:
public class SManagerTest extends AndroidTestCase {
private SManager sm;
public SManagerTest(){
sm = SManager.getInstance(getContext());
}
#Test
public void trainTest(){
sm.go();
}
}
What's wrong? because I get this:
Test run failed: Test run incomplete. Expected 1 tests, received 0
Thanks!
As pointed out earlier use "test" with a small t and don't forget to run it as an "Android JUnit Test"
Agree with Christopher - start the method name with 'test' even though you're adding the #Test decorator. Also, add this class in the same folder as your tests and it will run all your tests:
public class AllTests extends TestSuite
{
public static Test suite()
{
return new TestSuiteBuilder(AllTests.class).includeAllPackagesUnderHere().build();
}
}
So, I'm new to android unit testing. I'm trying to write a unit test for the Phone application:
package com.android.phone;
import android.content.Intent;
import android.net.Uri;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import com.android.phone.PhoneApp;
import dalvik.annotation.TestTargetClass;
#TestTargetClass(PhoneApp.class)
public class TestPhone extends ApplicationTestCase<PhoneApp> {
public TestPhone() {
super(PhoneApp.class);
}
private PhoneApp phone;
#Override
protected void setUp() throws Exception {
super.setUp();
phone = getApplication();
}
#MediumTest
public void testDialerIsUp() {
assertNotNull("Phone app does not exist", phone);
// TODO add tests
}
}
Then I start an emulator, wait till it boots up, and run those tests:
adb shell am instrument -e class com.android.phone.TestPhone -r -w com.android.phone.tests/android.test.InstrumentationTestRunner
And now I'm getting a junit.framework.AssertionFailedError: PhoneApp does not exist. What is wrong here, why isn't PhoneApp up?
Actually, I'd recommend calling createApplication() in your setUp() method before calling phone = getApplication().
You don't show the code for your PhoneApp. Did you derive a PhoneApp class from the android.app.Application class? Or are you expecting that there is just something called PhoneApp out there that you can test?
You will need to write an android.app.Application class as part of your project, if you expect to test something.
Or, perhaps, you are talking about something that I do not understand. That is always possible.
How does this even compile with "PhoneApp.class" in it if you just stick to the SDK?
I know you can use Robotium to test existing apps though.
Elaborating on Karim's answer (it does work), this is the setup method:
MyApplication application;
#Override
protected void setUp() throws Exception {
super.setUp();
createApplication();
application = getApplication();
}