Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing JUnit result to file

Tags:

java

junit

junit4

I want to print the results of my JUnit tests to a .txt file.

Following is my code:

try {
    //Creates html header 
    String breaks = "<html><center><p><h2>"+"Test Started on: "+df.format(date)+"</h2></p></center>";

    //Creating two files for passing and failing a test
    File pass = new File("Result_Passed-"+df.format(date)+ ".HTML");
    File failed = new File("Result_Failed-"+df.format(date)+ ".HTML");
    OutputStream fstreamF = new FileOutputStream(failed, true);
    OutputStream fstream = new FileOutputStream(pass, true);
    PrintStream p = new PrintStream(fstream);
    PrintStream  f= new PrintStream(fstreamF);

    //appending the html code to the two files
    p.append(breaks);
    f.append(breaks);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Following is my example testcase:

public void test_001_AccountWorld1() {

    // Open the MS CRM form to be tested.
    driver.get(crmServerUrl + "account");
    nameOfIFRAME = "IFRAME_CapCRM";

    PerformCRM_World1("address1_name", "address1_name", "address1_line1", "address1_postalcode", true);

    assertEquals(firstLineFromForm.toString(), "");
    assertEquals(secondLineFromForm.toString(), "Donaustadtstrasse Bürohaus 1/2 . St");
    assertEquals(postcodeFromForm.toString(), "1220");       

}

I've tried p.append() but doesn't work. Help please.

like image 990
Limpep Avatar asked Oct 13 '22 19:10

Limpep


1 Answers

In general , you can redirect your output to file as follows :
- if you are using eclipse : Run configuration-->Commons-->OutputFile-->Your file name enter image description here

  • If you run form the command line , just use : java ..... >output.txt
like image 193
Jalal Kiswani Avatar answered Oct 18 '22 02:10

Jalal Kiswani