I'm using Robolectric to test Android. I'm running my tests via maven, e.g.
mvn -Dtest=LogTest test
If I have code that writes to the logs, such as
Log.d("TAG", "blah");
or using Roboguice's Ln
Ln.d("blah");
I don't see any output in maven's surefire logs (text files).
Ideally, I actually want simple log statements to go to the console. I can write to the console by using System.out.println("blah")
, but of course I'd rather use the supported logging APIs.
So my question is, why am I not seeing log output at all, and how can I get the log messages written to the console?
Im using robolectric 2.3. How works for me:
Into my @Before:
Inside my test functions i can use (ShadowLog. have other options):
And inside my tested class I can put some messages at log with:
By default, logging output when using the RobolectricTestRunner disappears. You can configure where it goes by looking at the setupLogging() method of that class.
To summarize, you need to set the
robolectric.logging
system property to eitherstdout
,stderr
, or a file path where the log should be written. I do this in the constructor of a subclass ofRobolectricTestRunner
that I use for all tests so that logs always get written to stdout.The solution that worked out best for me (or at all) was to initialize a replacement injected implementation (during testing only) of RoboGuice's Ln.Print class to do System.out printing instead of Android's Log printing, given I was actually using Robolectric to avoid having to depend on the Android subsystem to run my tests in the first place.
From Ln.java:
So basically:
and:
That of course assuming the standard Robolectric init to hook the module up with RoboGuice:
Add the following to your test setup before your test runs:
https://groups.google.com/forum/?fromgroups=#!msg/robolectric/PK-9cQQQROw/svuQzM5h_vsJ
I am running robolectric-2.0-alpha-3.
What worked for me was to set in the setUp method of my test the stream to
stdout
Something like:
With this version of robolectric I had no success doing the same (
ShadowLog.stream = System.out
) in a custom TestRunner or in my TestLifeycleApplication.Setting the system property
System.setProperty("robolectric.logging","stdout");
was of no effect as well, but it might works in previous versions.When running tests with maven all you need is something like this :
When running the tests locally, e.g. in intellij, then all you need is an environmental variable: Just go (for intellij) to Run/Debug Configurations --> Defaults -->Junit --> VM options and add