I am running JUnit tests as part of my Android app tests. In this case it's just testing the responses of an API we rely on that may be changed frequently (in order to validate later tests)
When these tests pass the output window of Android studio shows no messages. We'd instead like to start each method with a message saying which endpoint it is testing (E.g. "Tests /oranges//segments") so that the message always shows as the first message for both passing and failing tests.
I tried the android logs Log.println(Log.ASSERT, "...", "...") but that did not show in the android studio output.
Is there a way to do this? The output window I refer to is the one displayed below
(When a test is marked as ignored, you can supply a message to display. I'd just like the same behaviour for my other tests)
The old fashioned Java styled System.out.println(); will work.
JUnit tests are not executed on a Android device (or emulator) but on Java Virtual Machine on PC's environment. You have two solution:
Just use some Logger (like log4j, JDK logger, etc) or simply use System.out.
Use Roboletric and its capability to define shadow classes to redefine Log class.
I suggest you the second way. Roboletric already have a shadow class for log. To redirect output into console
#Before
public void setUp() throws Exception {
ShadowLog.stream = System.out;
//you other setup here
}
Some references:
https://guides.codepath.com/android/Unit-Testing-with-Robolectric
https://github.com/robolectric/robolectric-samples
http://www.vogella.com/tutorials/Robolectric/article.html
I hope it helps.
Related
Q. How to capture screenshots while writing the UI test cases in android studio using espresso tool, kotlin language and robolectric test cases using act, arrange and assert
For mobile android application -
1. I am writing the test cases using android studio, kotlin language and robolectric form- act, arrange and assert android UI Test cases.
Running test cases on - emulator, real device
In act- login the app
In arrange - Go to specific screen and perform click function
In assert- validating the UI with specific fields and column text fields and value.
I want to capture the screenshots in between where the assert commands failed or code exit with an error. I was new to kotlin but now learnt how to write the code, need help in how to capture the screenshot in this case.
I had searched helping code on google and try out to find some hint but didn't get much to understand.
Below is the code -
#Test
fun loginSuccess() {
// arrange
// act
onView(withId(R.id.login)).perform(click())
// assert
onView(withId(R.id.logoImg)).check(matches(isDisplayed()))
}
You can refer to this article. There is not a single method which handles everything for you.https://medium.com/#mohitgupta92/custom-failure-handler-for-android-espresso-c8c99eb65a32
I am having trouble testing native applications (such as Contacts and Settings) using Cucumber-JVM for Android with JUnit. Initially, I got the following message:
"Test run failed: Permission Denial: starting instrumentation ComponentInfo???{com.test.contacts/android.test.InstrumentationTestRunner???} from pid=673, uid=673 not allowed because package com.test.contacts does not have a signature matching the target com.android.contacts"
To solve this problem, I signed the test application with the same keys of the Contacts native android application (shared.x509.pem and shared.pk8) and I also added the following line to the AndroidManifest.xml file (as suggested in How can I sign my application with the system signature key?):
“android:sharedUserId="android.uid.shared"
This seemed to solve the problem.
However, after this change, I only manage to run the first test from a test suite. When the second test is running, it gets lost in the getActivity() method from the class ActivityInstrumentationTestCase2, which my steps definitions class extends. More precisely, it doesn't get out of the mSync.wait() call in the method startActivitySync(Intent intent) from Instrumentation.java. The call to getActivity() calls launchActivityWithIntent(), from InstrumentationTestCase.java, which calls startActivitySync(Intent intent).
I found a similar issue on Why does getActivity() block during JUnit test when custom ImageView calls startAnimation(Animation)?, but the workaround described there doesn’t solve my problem.
My test application is really simple and it only checks the content of the buttons in the activity. I don’t have this problem if I use the same test application to test my own apps, only with native android applications such as Contacts and Settings.
Does anyone know something about this issue and could give me a light on how could I solve it?
Thanks in advance
I'm doing UI test automation on Android using Appium and py.test. I'd like to be able to save a bug report using adb after a test fails.
Is there a way to tell if a test fails in my test code so I can then run save the bug report in the teardown?
Originally, I was just going to save the bug report after each test, but it's a bit excessive adding 45 seconds to each test.
You can implement a pytest_runtest_logreport hook in your conftest.py like this:
def pytest_runtest_logreport(report):
if report.when == 'call' and report.failed:
# save bug report
For more information, see Woking with plugins and conftest files.
I'm new to Android development and just experimented with unit testing inside Android Studio. I have 2 questions,
each time I need to run tests I need to create a "Run/Debug Configurations" for the test class that derives from InstrumentationTestCase, and in this configuration I can only specify 1 test class and 1 test method at a time. Is there a way to break this limitation so I can run a bunch of test classes and their test methods altogether?
I have Log statements in my tests, but I could not find where it logged the output messages to, I checked logcat and event log but didn't find it there.
Thanks.
1) You should be able to right/control click either a test method, or a test class (suite) and run the test from the context menu.
After you run it once, the run config should be, permanently, in the list of runnable configurations, and available in all of the handy ways.
2) The most likely reason that your log messages are not showing up, is that Studio has this annoying habit of automatically installing a filter, on a run, so that it only shows log messages that are from the package of the app you are running. Since your test and your app are in different packages, it is probably filtering some of the messages away.
On Android I am using the android.util.Log to log within my application and during development I am using the adb logcat or Eclipse to see the logs - I use it even more then debugging...
On device I can save the logfile from my code or use some application form Android Market to save the logs - e.g. aLogCat.
Now can I do the same on the iPhone? I can use the NSLog(#"message");, but can I easily save the log file from my application and access it? Are there any ways for that?
Regards,
STeN
This is from NSFoundation reference
NSLog:
Simply calls NSLogv, passing it a variable number of arguments.
NSLogv:
Logs an error message to the Apple System Log facility (see man 3 asl). If the STDERR_FILENO file descriptor has been redirected away from the default or is going to a tty, it will also be written there. If you want to direct output elsewhere, you need to use a custom logging facility.
Thus, it is only a matter of redirecting the file-descriptor "stderr" (2) to a custom file, and you will get everything that you print using NSLog in that file.
This seems to be exactly what you want.
Note that if you want to get logs on console when you are connected to the debugger, you can wrap your code around this to avoid redirection in this case:
if (!isatty(STDERR_FILENO)) { // Not connected to any terminal
// your redirection code
}
You can access the console log from Organizer->Device->Your device->console.
If that is not powerful enough, consider using utilities like NSLogger.
The previous answers are good; also see this if you're inclined to making system calls.