Test Fragment with espresso lib - android

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/

Related

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

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".

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!

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(){
}
}

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");
}
}

Runtime Exception in Android JUnit testing

I have a simple HelloWorld Activity that I try to test with an Android JUnit test. The application itself runs like it should but the test fails with an
"java.lang.RuntimeException: Unable to resolve activity for: Intent { action=android.intent.action.MAIN flags=0x10000000 comp={no.helloworld.HelloWorld/no.helloworld.HelloWorld} }
at no.helloworld.test.HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)"
This is my activity class:
package no.helloworld;
import android.app.Activity; import
android.os.Bundle;
public class HelloWorld extends
Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} }
And the test:
public class HelloWorldTestcase extends ActivityInstrumentationTestCase2 {
private HelloWorld myActivity;
private TextView mView;
private String resourceString;
public HelloWorldTestcase() {
super("no.helloworld.HelloWorld", HelloWorld.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
myActivity = this.getActivity();
mView = (TextView) myActivity.findViewById(no.helloworld.R.id.txt1);
resourceString = myActivity
.getString(no.helloworld.R.string.helloworld);
}
public void testPreconditions() {
assertNotNull(mView);
}
public void testText() {
assertEquals(resourceString, (String) mView.getText());
}
protected void tearDown() throws Exception {
super.tearDown();
}
Why does the test fail? The activity is (of course) defined in AndroidManifest.xml and the application runs as it should.
The package in the constructor call should match the instrumentation target in the manifest. It should be "no.helloworld" instead of "no.helloworld.HelloWorld"

Categories

Resources