Android Test Case - how to view output? - android

I'm using AndroidTestCase for unit testing. I have a unit test which is failing, but I haven't figured out how to display output from it. For example, I don't see the values I print using Log.d or System.out.println in the console nor the logcat output.
How do I view the unit test console?

With Android Studio 2.3.1, works using the old fashioned Java styled
System.out.println("Start Test");
Here you can see the output:

You shoud see the Log.d() messages in logcat unless your test is crashing much before (i.e in the constructor).

In my case ,
System.out.println("Test") is working for JUnits tests,
Log.d("tag" , "Test") is working for AndroidJUnit tests(Instrumented)

Go to Debug --> Logcat. Make sure the LogLevel and Filter are correct as well.
See the screenshot from Android Studio 1.1.0

Just to be clear, if you're running an emulator you have to use adb logcat.
Also, make sure you're using the standard android.util.Log.d() to send your output.

You probably disconnected the device/emulator and reconnected. You have to click on the device name again in the list in the Dalvik Debug Monitor for it to reload the logs and keep updating the output.

Related

Log.d reports not showing up after Android Studio 2.3 Update

After upgrading to Android Studio 2.3, my Log.d reports are no longer showing up in the run menu. I rebooted my machine twice. Strangely, the reports are showing up when I run an app on my physical device, but not for the emulator.
goto Run - Edit Configurations
then 2nd tab Miscellaneous
check both items Show logcat automaticallyand Clear log before launch
According to this bug report, this seems to have been an intentional change in 2.3:
https://code.google.com/p/android/issues/detail?id=235668
This worked for me: the mistake I was making was leaving out the message. For example I was only providing the first string parameter (the tag) and leaving the actual message out:
Log.d("MyTag", "")
When I stopped putting an empty string as the 2nd param and putting a random Char, or anything for that matter in the 2nd param then it showed up in my LogCat, otherwise it got skipped.
Hope this helps someone!

how to work on android mobile logcat

if its possible to see what type of run time exception in android mobile? because i developed one application which works fine in emulator but stop working in mobile.. is there any Log-cat..
[a Log-cat]: https://play.google.com/store/apps/details?id=org.jtb.alogcat&hl=en doesn't shows errors..! i cant find any tutorials for "a Log-cat" apk.. please help me!! Thanks in adavance
You will find a lot of applications to read the Log of your installed Apps, but they require root permissions to display the exceptions.
If you have the sdk tools installed you have access to the log cat from that if you run your apk while connected to the computer. If you use eclipse there is even a guy with easy access to it, otherwise there are command line tools.
In essence, plug it into your computer and you can get to the logs.
I would recommend you work through:
https://developer.android.com/training/basics/firstapp/index.html?hl=cn
As it will get you set up and teach you to use the tools. If you don't want to do that:
Command Line:
http://developer.android.com/tools/help/logcat.html
Eclipse:
How to enable LogCat/Console in Eclipse for Android?
How to debug on a real device (using Eclipse/ADT)
IntelliJ
https://www.jetbrains.com/idea/webhelp/debugging-with-logcat.html
Short: Either root and use an application to view the log cat, or plug it into your computer.
you can try this code to view it
try{
//do something
}catch(Exception e){
Toast.makeText(context,e.message(),Toast.LENGTH_LONG).show();
//or you can start a new ACTIVITY and put STACKTRACE in a TextView
}
But this have a drawback that you should know that in which part of you app the Exception may occur.
I have tried it using a Toast. Hope it helps.

Where is standard output for android development on Netbeans?

I am developing an android app on netbeans. If, for example, I were to add System.out.println("Hello World") attached to a button click, it does not output anywhere. Am I missing something basic?
This also means I can never see if exceptions are being thrown or anything. If my app works it works and if not I am developing 'blind'. Help?
On Android all of those go to the on device logcat. Not sure how you'd see that with netbeans, but you'd need to hook it up to adb logcat in some fashion.

How to know which line the app crashed?

I'm debugging an app in a real device and Eclipse. Certain feature makes it crash. There is no error on LogCat. I've managed to find which line it crashed in a similar situation by writting Log.v in many lines until I found which Log.v didn't show up. Isn't there a better solution?
You could use the Debugging feature in Eclipse - it allows you to break the program flow on the occurrence of an exception (Run As -> Debug).
This will allow you to inspect the current local variables / call stack to further diagnose why the exception occurs.
p.s. One other method I use when hunting down an odd crash is to dump the full LogCat via the 'adb' CLI tool, and inspect it in a text editor. Sometimes the LogCat display in Eclipse can go a bit.. weird..especially if you've been connecting / disconnecting your device while developing (without closing Eclipse)
Check Logcat filters (in Eclipse). Happens to me.
I was using the 'debug' option on Eclipse, which made it not show the error. Detaching the debug after the crash, or just running the application from the begin (without the debug options) shows the error. To find the line, filter your LogCat by application, make the app crash and, on the red text that will appear, look for your package name. And the of the error (expection) itself is on the beginning of that text.

How to generate test result report using robotium?

I have been using robotium to test my android application.Testing was successful but i want to know that is there any way to view test results in a separate file.
I have had good luck just running the robotium tests as regular Android JUnit tests, and then using the standard mechanism to get back the test results, namely the ant task fetch-test-report. To do this I used the build.xml file create by android through the tooling. See documentation for developing in other IDEs for detailed instructions on setting that up. Good luck!
XML report generation is quite simple when we are using Eclipse IDE
Steps to be followed
Run the Junit test as usual in eclipse Run As->Android Junit Test.
Once the test run is completed you will be indicated with the status in the status bar provided by the IDE.
When you click on the Test Run History Icon which is available in the Junit view of eclipse IDE you will find the Export option through which you can you can export the XML format test.
If you have and problems in finding the icon please watch the below image.
I use approach with logcat and android class Log.'severity'(tag, msg) ('severity' can be i-info, d-debug, w-warn, e-error, v-verbose), here is an example of clicking button myView:
String viewName = solo.getString(R.string.myViewName);
try {
solo.clickOnView((View) solo.getView(R.id.myView));
Log.i("Passed", viewName + " works correctly.");
} catch (Exception e) {
Log.e("Error", viewName + " doesn't work.");
}
Using filters you can filter output by tags and selecting all lines save it in .txt file. All these you can do automatically by writing simple script which will redirect output to file from InstrumentationTestRunner for you.
see also pages: Log class, logcat
In my case, I user spoon to generate test html reports. https://github.com/square/spoon
You can follow the way of your choice because it is not pure junit code:
1-XML based test execution report using any xml writing/parsing apis
2-logcat based test execution report using logs for example if you are using Eclipse IDE
3-customized report writing to your device under test, for this you will need apis to write to device
These are three ways out of several available as per your requirements and IDE.
You can use the PolideainstrumentationtestRunner, it allows you to specify exactly where you want to store the test result. (add option junitXmlOutput true and junitOutputDirectory yourPath)
http://code.google.com/p/the-missing-android-xml-junit-test-runner/
I hope it will help.

Categories

Resources