ContentProvider starting service before Junit tests in test enviornment - android

I have a whole bunch of existing tests for an aidl service. The test class extends ServiceTestCase. They were all working fine until I recently added a new ContentProvider in the same package. Now when I run the tests one or two of the existing tests fail randomly.
The reason for this fail is that I am binding to the service from onCreate() of the ContentProvider. When the test framework restarts the process the ContentProvider is getting created automatically and it starts the aidl service BEFORE the test has executed setup. This results in the service being running even when I have not started it from the particular test. This causes timing issues which cause the random test to fail.
I tried out following things :
If I comment out the bind call from the onCreate of the new ContentProvider then all tests still pass successfully.
(Note : None of the existing tests needs this provider - I still need to add the tests for this provider)
I tried setting some boolean from tests and checking that in onCreate() of the provider - but that does not work since provider is initialized before the setUp() of the junit test
If I add startservice() in tests that did not call it previously then the service actually gets started 2 times and I can see 2 instances running during the unit test

Related

Custom Test Orchestrator or callback for start/finish

I'm using the Android Test Orchestrator to run my UI tests and i need to generate a report of the results when all the tests are finished.
I need to have a callback for when the orchestrator starts running the tests and when it finishes running all of them in a way that i can generate and save my report.
Given that a new instance of the AndroidJUnitRunner is created for every tests, i can't take advantage of onStart() and finish() to do that.
I was looking for a way to maybe provide a custom orchestrator so i can use the internal methods..

How to run tests in android

I am using Robotium Solo to test the app
since I'm fairly new to app test, and Robotium
I have 3 methods in my test case - however, I want to run those tests, under certain conditions
otherwise they fail
I can do that if i write the entire test in one method, but i don't want to run it in one method, I
want to separate it into 3 methods
how do I make sure that the tests run only if I called the test methods, and not one after the other
In Robotium Test Application, every public method starting with keyword 'test' will be considered as one test case.
So, you can try using 2-3 methods with names starting by keyword test.( e.g. testA(), testB() etc.)
So, even if testA() fails, automation testing will continue to run for testB().
And results will be shown to the JUnit viwe.
Note:- the test cases are executed in the alphabetical order as per their names.
Hope this helps , ask if you are stuck somewhere.

Robotium: How to continue running test cases without restarting the app after each test?

In my test class, i have multiple test cases written. Now when I run the test project, after each test case the app is getting stopped and started again. In teardown I am calling solo.finishOpenedActivities().
I want to run all test cases without closing the activities.
I tried to remove solo.finishOpenedActivities from the tearDown method. In that case the next testCases are not getting executed and the test hangs after the first one.
So what is the proper way to have multiple test cases and not close the activities and continue running all the tests?
If the functionalities between your test cases are dependent on each other, then instead of writing them in separate testcases, write different functions related to different functionality and call then in same testcase according to their order of execution.
Because, after completion of each testcase, robotium will definitely close the activity and start a new activity for the other testcase.

How to perform integration test on android (or how to get and inject several components in TestCase)

I have an application which has some services which communicate to each other. All of them work in a single process so they don't need a Messenger. While application runs, my main service connects to other ones and injects services as dependencies. For example ImportService needs ParserService to perform some operations and so ImportService has method setParserService(ParserService service);
It works fine at real operation but I can't run tests for services which need dependencies, because ServiceTestCase.bindService() only knows how to start/bind a service which it actually tests. It can't start/bind other service which I need as a dependency. It compiles when I try to get an other service but crashes while trying to call bindService().
I've googled a lot but now I can see only three ways of solving it:
Make an application more primitive. Use just classes instead of services;
Not to make injections. Get dependencies inside of a service instead. But this way I'll have to wait while dependencies(services) are bound at some places inside of a service. It will be a bad code. I can't use onServiceConnected() because sometimes I may need more than one service as a dependency.
Not to use tests :-)
So, briefly, the question is: I can bind one specific service in a test by using ServiceTestCase. But how can I bind 2, 10 or more services in one test?
ServiceTestCase provides a framework in which you can test Service classes in a controlled environment. It provides basic support for the lifecycle of a [single] Service, and hooks with which you can inject various dependencies and control the environment in which your Service is tested.
If you are performing integration test of various Services you should test them as part of the Activity or Application using them.

Parameterized instrumentation tests on Android

I'm trying to write a batched instrumentation test (using ActivityInstrumentationTestCase2) for a particular Activity where I change the intent each time the test runs. I can do this with a single test, and just loop through stopping and restarting the Activity with the new intent, but this is not what I want. One reason is they really should be separate test runs. The other reason is, I'm using Spoon to generate a report when the tests finish, and the report will rightly think I only ran one test.
What I would like is it to treat a single test as a possibly infinite number of tests, and pass the data into the test each time the test runs.
Unfortunately you can't use Theories because it results in a RuntimeException where the InstrumentationTestRunner can't find my tests. Anyone have any luck with this?
You could always just create a "testing" intent. In order to simulate the relaunching of the application, make a method or several methods that reset all your static variables between tests. Then you can test the classes from within a testing intent inside the application itself using
assert("value", MyClass.myMethod);
resetStatics();
assert(true, MyClass,myMethod);
resetStatics();
I don't know how much this will help you, if at all, but this is how I started writing my own tests.
I recently discovered that you can add a public static Test suite() method to a test class, and when you run just this single test class, InstrumentationTestRunner will run the Test returned by this method. This is helpful because suite() can explicitly call any constructor of your TestCase, including one with parameters.

Categories

Resources