Being Tester we are used to reporting bugs and we know that developer debug the code to find out the line of code causing the bug. But now you are the one coding the test cases and you are responsible for solving any bug in your testing framework.
Android Studio provides some shortcut keys for debugging. In this post, we will use our HelloWord Espresso test Case Example and We will run our test Case In Debug mode and we will debug our code line by line.
It is always good practice to use Debug Mode for root tracing the bug as it allows you to run your code line by line by setting a debug point. Debug point is the nothing but marking the line of code from which you wish to start debugging. This speeds up your work and efficiency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.example.globalsqa.myapplication1; import android.app.Activity; import android.app.Instrumentation; import android.app.LauncherActivity; import android.content.Intent; import android.support.test.InstrumentationRegistry; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnitRunner; import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityUnitTestCase; import android.test.InstrumentationTestRunner; import android.test.suitebuilder.annotation.SmallTest; import android.view.ContextThemeWrapper; import android.widget.TextView; import com.example.globalsqa.myapplication1.MainActivity; import com.robotium.solo.Solo; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; public class MyActivityTest extends ActivityInstrumentationTestCase2<Activity> { private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.example.globalsqa.myapplication1.MainActivity"; private static Class<?> launchActivityClass; static { try { launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } private Solo solo; public MyActivityTest() { super((Class<Activity>) launchActivityClass); } @Override public void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); } @Test public void testText() throws Exception { // s=new Solo(this.getInstrumentation(),getActivity()); solo.waitForView("MainActivity"); Thread.sleep(3000); TextView t = (TextView) solo.getCurrentActivity().findViewById(R.id.globalsqa); assertEquals("Hello world!", t.getText().toString()); } public void tearDown() throws Exception { solo.finishOpenedActivities(); super.tearDown(); } } |
This will help you debug your test cases and write a efficient and bug free code.
So far we have learnt on creating our Android Studio project and overview of Junit and TestNG testing framework. In this Tutorial, we will write our first test case and run it. It sounds exciting but before that we need to decide a testing tool which we want to use for writing testing Framework.
We have many famous testing tools in market like Appium, Robotium , Espresso etc. Espresso is an open source framework provided by Android Studio so let use that to write our first test case.
The main purpose of this tutorial is to learn to write and run test case in Android Studio so we will focus on this and not on testing tools. We will learn about testing tools in future posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@RunWith(AndroidJUnit4.class) @LargeTest public class EspressoTest1 extends ActivityInstrumentationTestCase2<MainActivity>{ public EspressoTest1() { super(MainActivity.class); } @Before public void setUp() throws Exception { super.setUp(); injectInstrumentation(InstrumentationRegistry.getInstrumentation()); } @Test public void test1ChatId() { getActivity(); onView(withId(R.id.helloworld)).check(matches(isDisplayed())); } @After public void tearDown() throws Exception { super.tearDown(); } } |
1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2. The class extends AcitivtyInstrumentationTestCase2 class with launch activity as MainActivity which we want to test in this example.
3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
[email protected] annotation is added for each test case method.
5. In the below test case the getAcitivity() launches our Main Activity.
6. onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.
In our previous tutorial, we have learnt about Junit Testing framework and now in this we will get to know about one more powerful testing framework called TestNG. TestNG framework as some different functionalities which makes it more powerful testing framework..
TestNG is a testing framework developed from inspiration of Junit and Nunit with additional features making is more effective and simple for use. It is a powerful automation testing framework where NG Stands for Next Generation. It gives developers power to write more flexible and powerful test cases.
There is so much more in this and we will explore more and more when we start coding. But it is very important to learn basics about Testing frameworks before writing your test cases as selection of right framework is like building pillar for your automation framework.
1 |
testCompile 'org.testng:testng:6.9.10' |
In our previous post, we talked about the Android Studio and Java Installation. In this we will learn about very famous unit testing framework setup for android studio that is Junit. Before jumping on to the setup let us learn more about Junit Testing Framework.
Junit is a widely used testing framework used for Java Programming language. Before you learn, how to write automated test cases it is very important to learn Junit testing framework in detail. It helps you to define the flow of your testing framework.
Junit is built on idea of “first testing and then coding” which helps in increasing productivity of the test case developer and stability of the code.
A Manual tester want to start and learn automation, there are many curiosity come to their mind about what tool to use?, which IDE(Integrated Development Environment) to select?, what testing framework to select? This tutorial will help you to know about an IDE called Android Studio. IDE is nothing but development environment for writing and executing code.
Android Studio is the official IDE for coding android app, but it is also very good alternative for writing and executing test cases. Android Studio is very easy to install and faster to write, deploy and run.
After knowing, how we can effectively use Android Studio for our testing effort. Now, we will see how to install Android Studio. Before installing Android Studio, it is required to install Java.
Android Studio allows you to write unit test as well as instrumentation tests. It is very important for us to learn more about unit tests and instrumentation tests. We have defined it in terms of Android Application.
In layman terms, Unit tests are the test cases written for testing code written by developers and Instrumentation test are the test cases written for testing UI & functionality of the application. In short, Unit tests is associated with white box testing and instrumentation tests is associated with black box testing.
Coding with android studio looks very promising because it is an IDE based on IntelliJ IDEA that is used for android application development. Even though we are not going to build android application , it is very important for us to understand the project structure so that we can code effective and understandable automation test framework.