I am working in Android app that is already developed and I need to write test case for this app in Android Studio. I want to know how to write test case in Android Studio ?
I also want to know what is main propose of writing test case? How to test app that we are writing write test cases?
I have made test folder
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
private static final String LOGIN_DIALOG_FRAGMENT_TAG = NestAlertDialog.class.getSimpleName();
private LoginActivity mActivity;
private MainActivity mMainActivity;
public LoginActivityTest() {
super(LoginActivity.class);
}
I have made test folder
Atm(Android Studio 1.1.0) when you create a project AS takes care of creating a testing folder inside your app under /app/src/androidTest.
You can still create a test project directory elsewhere if you prefer, or you can try to follow the official website notes on testing.
I want to know how to write test case in android studio
I suggest you to start by downloading the sample code activityInstrumentation. From Android Studio, File -> Import Sample.
Later you can check the documentation to develop further and more complicated test.
You can check this as further reference too.
What is main propose of writing test case
If your program is simple and does not goes beyond 100 LOC test case are probably useless, just the bigger the project gets the harder is to debug. You can have test cases for every tiny piece of code composing your application, thus ensuring that your entire application works correctly.
You can also write your application by writing its test cases before a single line of code as with Test Driven Development.
Related
Is there a way to create an espresso test file from a class like does Command / Control+Shift+T?
Same for launch the current test with espresso into the device?
It's very annoying to create/launch every test manually through the whole process of going to the file's hierarchy and then create the launch configuration of it. Neither an option is hidden in options?
If I remember well, these functionalities were available on Android Studio 2.3, I noticed the lack of them from AS3.0. Where did they go?
As today, Android Studio 3.1.1 comes with the functionality just as before
So I am running a few tests in AndroidJunit, which tests a manager class that I created for my application. When I add breakpoints to the Test class and go into debug mode, they get hit just fine. But if I add break points to the Manager class that I am testing in junit, they do not get hit when I run the tests. Is it possible to hit break points in a non-test class when running junit in Android Studio? I'd appreciate any suggestions on how to solve this issue. Logs also do not show up when I place them outside of the Test class.
EDIT/UPDATE
So I have been able to dig a little deeper and found out that the problem here was using Mockito with my Tests. So if I replace InstrumentationRegistry.getContext() with context = Mockito.mock(Context.class); then the debugger no longer hits the breakpoints placed in the manager. However, I need to use Mockito with my tests, so I'm in a bit of a predicament. Does anyone if there is a manner that I can use Mockito in that allows me to set and reach break points in a non-test class? Thank you.
FYI, I am passing context into the manager class method like this
manager.checkSharedPreferences(context), and the manager is extracting values from shared preferences.
Firstly I want to make a confession. I've never written a test before. I've been a programmer for more than 10 years, and never once I found the need to write a proper test (or whatever it is called) since mostly I write codes that (I think) can be easily tested manually.
Now I'm writing a pretty complex android app, and this manual testing I'm doing to make sure every functions and classes runs as intended slows me down miserably. So now I'm kinda searching in the dark on how to make my codes test-compatible (is there such a thing?) and where should I start.
I'm using the latest Android Studio (1.2 Beta 3). I found that under 'src' folder, there's an 'androidTest' folder, which (few folders beneath it) contains a file, ApplicationTest.java. Here's the content of ApplicationTest.java
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
Ok now back to my app. I want to test the class AnalyzerOffline.java (located under main>java>com.code.imin.app) that I've written, because it has pretty complex and large codes going around there. So how should I start? I tried reading http://developer.android.com/tools/testing/testing_android.html , http://rexstjohn.com/unit-testing-with-android-studio/ etc but I still don't know where to start - I feel like I'm missing something here, or maybe somehow my mindset of writing test or the whole idea of it are wrong.
So can please someone show me some light here?
I am using Monkey tool testing
Step 1:
open the android studio terminal(Tools-> open terminal)
Step 2:
In order to use monkey , open up a command prompt and just naviagte to the following directory.
export PATH=$PATH:/home/adt-bundle-linux-x86-20140702/sdk/platform-tools
Step 3:
add this monkey command into terminal and press enter..
see the magic in your emulator.
adb shell monkey -p com.example.yourpackage -v 500
500- it is the frequency count or the number of events to be sent for testing.
you can change this count..
More reference,
http://www.tutorialspoint.com/android/android_testing.htm
http://androidtesting.blogspot.in/2012/04/android-testing-with-monkey-tool.html
I would like to write an Espresso test that I can run on an application from outside that application. I don't have much experience with Android, but, as I understand it, a UI test is generally written and executed from inside the project of the application that it is supposed to test. For instance, according to the file explorer in Android Studio, the java files for a test application I was developing are stored in com.package.example.name, and the java files for the UI test are stored in com.package.example.name (androidTest).
I want to develop a UI test for an application (Application A) for which I do not have the project files. As I do not have the project for Application A, I cannot develop the test for Application A from inside that same project like I normally would. Thus, I have to somehow create and run it from outside.
My initial thought was to create a test application (Application B) from scratch and add a button to it that, when pressed, will open Application A. I would develop an Espresso test from inside Application B, have it press that button to launch Application A, and then have it follow other Espresso instructions I would develop by viewing the R.ids of Application A's UI elements through uiautomatorviewer.bat, included in the Android SDK tools.
At least to me, this seemed like it would work just fine in theory, so I went forward with the plan. However, when the button inside Application B is pressed by the UI test and Application A launches, the test cannot find an activity to continue testing on. No error is explicitly thrown; Logcat prints messages like "W/RootViewPicker﹕ No activity currently resumed - waiting: 2000ms for one to appear," repeatedly, where 2000ms increases as time goes on.
Historically, I have not had any problems when running Espresso tests across activity changes. I assume this issue has something to do with the fact that the activity being launched exists outside of the project where the Espresso test is based.
If anyone knows of a way to get an Espresso test to run successfully on an application when based in a project located outside of that application, I'd appreciate any advice you may be able to offer. I think I'm on the right track, but I don't know for sure. Thanks!
I created an Android test project using instrumentation and it works find with eclipse running as Android Junit test. It also generated apk file.
My question is how do I run this test from another android application instead of from eclipse. For example, if the test project is named T and I have another Android application named A. Then I would like to open up T from A.
Thanks in advance..
I think it will be good if you try to call the appropriate intent on a button click or on whatever you want to start the program. For example:
startActivity(new Intent("com.yourprogram.T") I'm not sure but i think it will works.
Why do you need to open Test project from an app on Device. I dont think you can do it because Android does not allow any app to inject action or call functions from other apps.