Print JUnit result for file

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

Below 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(); } } 

Below is my testcase example:

 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 tried p.append() but it does not work. Help me please.

+4
source share
4 answers

You are probably reinventing the wheel again. ANT , Maven , the X build tool, or your CI server should do this for you.

+1
source

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

  • If you run the command line form just use: java ..... >output.txt
+1
source

I believe that this functionality already exists. Read this part in the JUnit FAQ section.

0
source

When I try to do this, I launch it on the command line using a special runner that launches its own set. Very simple, almost no code. There is simply a test in the package that you want to run, and the runner below. You can see the logic there for a listing. My code just prints the errors, but you can easily adapt it to print the whole file. In fact, you simply look in the collection of objects of the results of failures and successes.

 public class UnitTestRunner { static JUnitCore junitCore; static Class<?> testClasses; public static void main(String[] args) { System.out.println("Running Junit Test Suite."); Result result = JUnitCore.runClasses(TestSuite.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println("Successful: " + result.wasSuccessful() + " ran " + result.getRunCount() +" tests"); } } 
0
source

All Articles