It seems that setup() is not called when the test is running - android

I am developing a test class for my application. The test class code looks like this:
public class ProfileActivityTest extends ActivityInstrumentationTestCase2<ProfileActivity> {
ProfileActivity profileActivity;
#SuppressLint("NewApi")
public ProfileActivityTest(Class<ProfileActivity> activityClass) {
super(activityClass);
// TODO Auto-generated constructor stub
}
#SuppressLint("NewApi")
public ProfileActivityTest() {
super(ProfileActivity.class);
}
protected void setup() throws Exception {
super.setUp();
profileActivity = getActivity();
}
public void test_profileActivityLoggingIn() {
assertNotNull(profileActivity);
assertEquals(View.GONE, profileActivity.findViewById(R.id.btnAddOrEdit).getVisibility());
}
protected void tearDown() throws Exception {
super.tearDown();
}
}
The problem is that I get assertionFailure on assertNotNull(profileActivity) though profileActivity is instantiated in setup(). I don't understand what I am doing wrong;

As #njzk2 has mentioned, "setup" should be changed to "setUp".

Related

run setUp() and tearDown() methods for each test suite InstrumentationTestCase Android

I'm implementing a Test automation tool and I have a class which extends InstrumentationTestCase. For example:
public class BaseTests extends InstrumentationTestCase {
#Override
protected void setUp() throws Exception {
super.setUp();
Log.d(TAG, "setUp()");
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
Log.d(TAG, "tearDown()");
}
public void test_one() {
Log.d(TAG, "test_one()");
}
public void test_two() {
Log.d(TAG, "test_two()");
}
}
When I run the tests of BaseTests, the setUp() method is called 2 times. One time before executing test_one() and another after test_two(). The same happens with the tearDown(), it is called after executing each of both two methods.
What I would like to do here is to call setUp() and tearDown() methods only one time for the execution of all BaseTests tests. So the order of the method call would be like:
1) setUp()
2) test_one()
3) test_two()
4) tearDown()
Is there a way to do such thing?
I resolve this problem by using:
#BeforeClass
public static void setUpBeforeClass() throws Exception {
}
and:
#AfterClass
public static void tearDownAfterClass() throws Exception {
}
instead of setUp() and tearDown().
So in your case it would be:
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class BaseTests extends InstrumentationTestCase {
#BeforeClass
protected static void setUp() throws Exception {
//do your setUp
Log.d(TAG, "setUp()");
}
#AfterClass
protected static void tearDown() throws Exception {
//do your tearDown
Log.d(TAG, "tearDown()");
}
public void test_one() {
Log.d(TAG, "test_one()");
}
public void test_two() {
Log.d(TAG, "test_two()");
}
}
The annotations #BeforeClass and #AfterClass assure that it will run only one time before and after the test runs respectively
I ended up following the idea of #beforeClass and #afterClass.
However I couldn't use the annotations itself. Instead, I implemented them (by using counters) on a base class and my test suites inherits from this base class.
Here's the link I based myself to do so:
https://android.googlesource.com/platform/frameworks/base/+/9db3d07b9620b4269ab33f78604a36327e536ce1/test-runner/android/test/PerformanceTestBase.java
I hope this could help someone else!

Test Fragment with espresso lib

My app has only one activity and based on many fragments. How I can test this fragment in a right way? Give me an example, please.
Cause I try this test class:
#LargeTest
public class ActivityTest extends ActivityInstrumentationTestCase2<ActivityEx> {
public ActivityTest() {
super(ActivityEx.class);
}
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testTest() {
//simple example
assertEquals(true, true);
}
}
And in result I've failed due to ClassCastException.
Rather do it like this:
public class ActivityTest extends android.test.ActivityInstrumentationTestCase2
{
public ActivityTest()
{
super(ActivityEx.class);
}
#Override
protected void setUp() throws Exception
{
super.setUp();
getActivity();
}
public void testTest() {
//simple example
assertEquals(true, true);
}
}
If you interested I also posted a tutorial on testing fragments http://www.stevenmarkford.com/testing-fragments-with-android-espresso-basic-example/

Espresso testing android

I run the tests in Android Studio, running task - "test". My test failed, my html report detailed:
junit.framework.AssertionFailedError: Class Test has no public constructor TestCase(String name) or TestCase()
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.TestCase.fail(TestCase.java:227)
at junit.framework.TestSuite$1.runTest(TestSuite.java:100)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
My source file:
#LargeTest
public class Test extends ActivityInstrumentationTestCase2<MainActivity>{
public Test(Class <MainActivity> activityClass) {
super(activityClass);
}
#Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testCheck(){
onView(withId(R.id.text))
.check(matches(withText("Hello world!")));
}
}
The problem is exactly what the error message says it is: You did not provide a parameter-free constructor.
Add this constructor to your test class:
public Test() {
super(MainActivity.class);
}
The prototype of a test class with espresso should be:
public class MainTest extends
ActivityInstrumentationTestCase2<MainActivity>{
public MainTest() {
super(MainActivity.class);
}
#Override
public void setUp(){
getActivity();
}
#Override
public void tearDown(){
}
public void test1(){
}
public void test2(){
}
}

Robotium: Activity not launched for second test

I have been able to run an android junit test with one test method successfully, but when more than one test method are involved, it just runs the first test and after tearDown, the activity does not relaunch for the subsequent tests. As a result, all my test methods fail, save the first one.
On debugging, I noticed that setUp method launches the MainActivity successfully before running the first testMethod, but on being revisited before the start of second testMethod, the same activity does not get relaunched. The code is as below:
package PACKAGE.test;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
public class Login extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "*.*.MainActivity";
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
#SuppressWarnings("unchecked")
public Login() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo solo;
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
#Test
public void testLoginScreen() {
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(2);
}
#Test
public void testSearch(){
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(1);
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
}
You may need to #Override your teardown method
#Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}

how to organize class in a robotium project?

I have an activity A that launch an activity B.
I'd like to have a robotium project to test my app so I'v created a first test class for activity A and all goes well.
I'd like now to create another test class for testing Activity B but it require some init from activity A.
I tried this:
BTestClass extends ActivityInstrumentationTestCase2 {
private Solo solo;
private ATestClass testA;
#Override
protected void setUp() throws Exception {
Log.i(TAG, "setUp");
solo = new Solo(getInstrumentation(), getActivity());
testA = new ATestClass();
testA.setUp();
testA.testAddAccount();
solo.clickInList(0);
}
[… more test method]
}
I got a NullPointerException when testA is doing getActivity()
I do it this way:
public class BTest extends ActivityInstrumentationTestCase2<SplashScreenActivity> {
protected static final String TARGET_PACKAGE_ID = "app.under.test";
protected Solo solo;
public BTest() {
super(TARGET_PACKAGE_ID, StartingActivity.class);
}
#Override
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
// setup stuff
}
#Override
public void tearDown() throws Exception {
// teardown stuff
super.tearDown();
}
}
All testcases just inherit from BTest then
public class OneTest extends BTest {
public void test_OneTest() {
//test stuff
solo.clickOnButton("Ok");
}
}

Categories

Resources