Several tests/test cases in one project using Robotium - android

I'm writing many tests for my Android application, meaning that there are about 15 tests cases. how can I implement them all? I tried to make several .java files in the same project for each testcase, but it ran only the first one. Then I made one Test,java and wrote several method inside it. like public void test1() throws Exception{...} public void test2() throws Exception{...} . But it also ran just the first test case. In Run Configurations I chose Run all tests in the selected project, while running I could see them all under JUnit window on the left part of the screen, it successfully ran the first, showed that the next is in process but it did nothing((((

Remember to use solo.finishOpenedActivities() in your tearDown(). Then the execution will not hang.

if you are using robotium to perform black box testing this is how ur class should look like:
public class TestAPK extends ActivityInstrumentationTestCase2 {
private static final String TARGET_PACKAGE_ID="com.android.example";//your package name
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.android.example.MainActivity"; //your main activity full class name
private static Class launcherActivityClass;
static{
try{
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}catch(ClassNotFoundException e){
throw new RuntimeException(e);
}
}
public TestAPK() throws ClassNotFoundException{
super(TARGET_PACKAGE_ID,launcherActivityClass);
}
private Solo solo;
protected void setUp() throws Exception{
solo=new Solo(getInstrumentation(),getActivity());
}
public void test1() throws Exception{...}
public void test2() throws Exception{...}
}

Related

How to clean db with ProviderTestCase2 or RenamingDelegatingContext

I do not understand what exactly should I do in order to get a clean and a different db from the one that the app uses.
This is my test class:
public class SQLTest extends ProviderTestCase2{
private static String testDbPrefix = "unitTest_";
public SQLTest (){
super(MyContentProvider.class, MyContract.CONTENT_AUTHORITY);
}
#Override
#Before
public void setUp() throws Exception {
//setContext(InstrumentationRegistry.getTargetContext());
RenamingDelegatingContext context = new RenamingDelegatingContext(InstrumentationRegistry.getTargetContext(), testDbPrefix);
setContext(context);
super.setUp();
}
#Test
public void test1(){
//test logic
}
}
I noticed that it always runs on the db that the app uses, even though I'm using a both ProviderTestCase2 and RenamingDelegatingContext, which are supposed to ensure I'm running with a clean db.
Can anyone explain please what am I missing???
Thanks in advance!

startActivity are not working in Android Unit Tests

I've tried everything already. The startActivity are not receiving the intent. Here's the code:
public class ColaboradorTeste extends ActivityUnitTestCase<ColaboradorMainActivity> {
private UserDAO userDAO;
private ColaboradorMainActivity activity;
public ColaboradorTeste() {
super(ColaboradorMainActivity.class);
}
#Override
protected void setUp() throws Exception{
super.setUp();
startActivity(new Intent(getInstrumentation().getTargetContext(), ColaboradorMainActivity.class), null, null);
activity = (ColaboradorMainActivity)getActivity().getApplicationContext();
userDAO = new UserDAO(activity);
}
public void testBase() throws Exception{
...
}
}
Here's the error:
java.lang.NullPointerException
at br.fsw.seatafmobile.ColaboradorTeste.setUp(ColaboradorTeste.java:23)
I know the problem is in the startActivity, but I really don't know how to solve it.
If anyone could help I'll really appreciate.
I finally found the solution!
My test is using a database created on the app. So I had to change the Build Variants from Unit Test to Android Instrumentation Tests.

Android Eclipse. Test class not found in select project

I create test project. My steps:
File - New -Project;
Choose - Android Test Project;
Set name's project;
Choose project when I want test;
Choose SDK target version;
Click Finish.
Done, I create libs folder and add to it espresso-1.0-SNAPSHOT-bundled.jar. Project's structure looks
Done, I create test class:
public class TestT extends ActivityInstrumentationTestCase2<MainActivity>
{
public TestT(Class<MainActivity> activityClass)
{
super(activityClass);
}
#BeforeClass
public static void setUpBeforeClass() throws Exception
{}
#AfterClass
public static void tearDownAfterClass() throws Exception
{}
#Before
public void setUp() throws Exception
{}
#After
public void tearDown() throws Exception
{}
#Test
public void test()
{
fail("Not yet implemented");
}
#SmallTest
public void testTest()
{
Espresso.onView(ViewMatchers.withId(R.id.btnClick)).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withId(R.id.tvClick)).check(ViewAssertions.matches(ViewMatchers.withText(MainActivity.TEXT)));
}
}
Done, I run test project:
Right click on test project;
Run as - Android JUnit project;
Project is run, but it isn't any displaying.
I open Window - Show View - Java - JUnit:
Double click on emulator-5554 show me dialog:
I try in all version's Eclipse and ADT Plugin. What am I doing wrong?
You are using JUnit 4. You must use JUnit 3 as Espresso is based on Android Instrumentation which currently only supports JUnit 3. To do this:
Remove the JUnit 4 reference from the project
Update syntax of test cases to Junit 3 i.e. remove the #Test annotations and update your test methods to be prefixed with the word "test"
Example:
Incorrect
#Test
public void test()
{
fail("Not yet implemented");
}
Example:
Correct
public void test()
{
fail("Not yet implemented");
}
You need to use GoogleInstrumentationTestRunner. See instructions here: https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions
Also, change your test class constructor to this:
public TestT()
{
super(MainActivity.class);
}

ActivityInstrumentationTestCase2 and use of static finals: fields become null after first test

This is really looks like some magic is going on and I'm interested to understand why that happens :)
Here's the unit-test I have:
public class SelectThemeActivityTest
extends ActivityInstrumentationTestCase2<SelectThemeActivity> {
private final static int[] STATIC_ARRAY = { 0, 1, 2 };
public SelectThemeActivityTest() {
super("com.the7art.simplewallpaper", SelectThemeActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
// some array usage here - will throw NullPointerEcxeption on second test
// see description below
STATIC_ARRAY[0] = 2;
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testFirst() {
}
public void testSecond() {
}
public void testThird() {
}
}
If I run this test case the first test completes successfully and all the rest fail by throwing NullPointerException from setUp() - the line which tries to access STATIC_ARRAY.
What puzzles me even more is the fact that if I change the test case to extend AndroidTestCase instead of ActivityInstrumentationTestCase2, then all tests complete successfully! Magic! :-)
Also if I remove 'static' keyword from STATIC_ARRAY, tests succeed too.
So it's clear that something is modifying my STATIC_ARRAY by making it null between a test runs, most probably in tearDown() and that something has to do with ActivityInstrumentationTestCase2, but how to track that something? :-) Any ideas?
The reason is scrubClass() method called from super.tearDown(): google-groups discussion. A solution is - overriding this method.
Put a watch point on STATIC_ARRAY and see who modifies it, although there are not too many candidates (since the field is private, there is pretty much only one candidate, the class you just posted, so something is missing from the picture.

Access image in my Android Tests

I am working on an Android app. It has corresponding spec/test application. As part of some of my tests, I need to pick up an image from my assets folder and calculate SHA-1 for it.
I can calculate SHA, as long as I can pick the image. Since the tests run on emulator; I am not sure how to pick the image in my test.
Does anyone have any idea, how I can go about it. With and without AssetManager maybe? Any ideas will be helpful.
Cheers
-Priyank
I do this by extending ActivityInstrumentationTestCase2, then in the setup getting a reference to the activity and finally getting the AssetManager.
public class Sha1Test
extends ActivityInstrumentationTestCase2<MyActivity> {
private AssetManager m_assetManager;
public Sha1Test() {
super("com.example.test", MyActivity.class);
}
#Override
public void setUp() throws Exception {
super.setUp();
MyActivity activity = this.getActivity();
m_assetManager = activity.getAssets();
}
#Override
public void testSomething() throws Exception {
InputStream stream = m_assetManager.open("myimage.png");
}
}
You'll need to run this on the emulator as an Android Unit Test, since it depends on the main activity to load the assets.

Categories

Resources