i wrote unit tests check whether logging working intended. example, here 1 of methods being tested:
// sampleobject class public void configure(context context) { log.d(tag, "configuration done."); }
i want flush logcat before each test logs previous test not picked up. that, wrote clearlogs()
method called setup()
:
private void clearlogs() throws exception { runtime.getruntime().exec("logcat -c"); } public void setup() throws exception { super.setup(); clearlogs(); sampleobject msampleobject = new sampleobject(); }
and here unit test:
public void testconfigure() throws exception { string log_configuration_done = "configuration done."; boolean stringfound = false; msampleobject.configure(new mockcontext()); process process = runtime.getruntime().exec("logcat -d sampleobject:d *:s"); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(process.getinputstream())); string line = ""; while ((line = bufferedreader.readline()) != null) { if (line.contains(log_configuration_done)) { stringfound = true; break; } } asserttrue("log message configure() not found", stringfound); }
the issue log entry cleared though clearlogs()
called in setup()
. test works if comment out clearlogs()
. have idea i'm doing wrong?
after several weeks, i've discovered cause: logcat -c
not clear logs takes several seconds so. however, process appears test application's point of view. result, test case executed afterwards , runs while logs still being cleared. adding short delay of 2 seconds resolved problem.
Comments
Post a Comment