How to use testFailed method of junit.textui.TestRunner class

Best junit code snippet using junit.textui.TestRunner.testFailed

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package com.dragome.tests;
2
3import junit.framework.Test;
4import junit.textui.TestRunner;
5
6public class TestSuiteRunner
7{
8	public static void main(String[] args)
9	{
10		TestRunner testRunner= new TestRunner()
11		{
12			public synchronized void startTest(Test test)
13			{
14				super.startTest(test);
15			}
16
17			public void testFailed(int status, Test test, Throwable t)
18			{
19				System.out.println("FAIL!!");
20				super.testFailed(status, test, t);
21			}
22		};
23
24		testRunner.doRun(DefaultTestSuite.suite());
25	}
26}
27
Full Screen
copy
1/*******************************************************************************
2 * Copyright (c) 2011-2014 Fernando Petrola
3 * 
4 *  This file is part of Dragome SDK.
5 * 
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the GNU Public License v3.0
8 * which accompanies this distribution, and is available at
9 * http://www.gnu.org/licenses/gpl.html
10 ******************************************************************************/
11package com.dragome.tests;
12
13import junit.framework.Test;
14import junit.framework.TestSuite;
15import junit.textui.TestRunner;
16
17import com.dragome.guia.GuiaVisualActivity;
18import com.dragome.tests.bugs.TestTryBug;
19import com.dragome.web.annotations.PageAlias;
20
21@PageAlias(alias="tests")
22public class TestPage extends GuiaVisualActivity
23{
24	public void build()
25	{
26		TestRunner testRunner= new TestRunner()
27		{
28			public synchronized void startTest(Test test)
29			{
30				super.startTest(test);
31			}
32
33			public void testFailed(int status, Test test, Throwable t)
34			{
35				System.out.println("FAIL!!");
36				super.testFailed(status, test, t);
37			}
38		};
39		TestSuite testSuite= new TestSuite();
40		testSuite.addTestSuite(TestTryBug.class);
41//		testSuite.addTestSuite(TestLambda1.class);
42//		testSuite.addTestSuite(TestDefaultMethods.class);
43//		testSuite.addTestSuite(LambdaTest.class);
44		testRunner.doRun(testSuite);
45		
46//		TestLambda1.main(null);
47//		
48//		StreamTests.main(null);
49//		TestStreams1.main(null);
50//		TestDefaultMethods.main(null);
51//		DragomeCompilerTests.main(null);
52//		try
53//        {
54//	        Test1.main(null);
55//        }
56//        catch (Exception e)
57//        {
58//	        // TODO Auto-generated catch block
59//	        e.printStackTrace();
60//        }
61	}
62}
63
Full Screen
copy
1package junit.textui;
2
3
4import java.io.PrintStream;
5
6import junit.framework.*;
7import junit.runner.*;
8
9/**
10 * A command line based tool to run tests.
11 * <pre>
12 * java junit.textui.TestRunner [-wait] TestCaseClass
13 * </pre>
14 * TestRunner expects the name of a TestCase class as argument.
15 * If this class defines a static <code>suite</code> method it
16 * will be invoked and the returned test is run. Otherwise all
17 * the methods starting with "test" having no arguments are run.
18 * <p>
19 * When the wait command line argument is given TestRunner
20 * waits until the users types RETURN.
21 * <p>
22 * TestRunner prints a trace as the tests are executed followed by a
23 * summary at the end.
24 */
25public class TestRunner extends BaseTestRunner {
26    private ResultPrinter fPrinter;
27
28    public static final int SUCCESS_EXIT= 0;
29    public static final int FAILURE_EXIT= 1;
30    public static final int EXCEPTION_EXIT= 2;
31
32    /**
33     * Constructs a TestRunner.
34     */
35    public TestRunner() {
36        this(System.out);
37    }
38
39    /**
40     * Constructs a TestRunner using the given stream for all the output
41     */
42    public TestRunner(PrintStream writer) {
43        this(new ResultPrinter(writer));
44    }
45
46    /**
47     * Constructs a TestRunner using the given ResultPrinter all the output
48     */
49    public TestRunner(ResultPrinter printer) {
50        fPrinter= printer;
51    }
52
53    /**
54     * Runs a suite extracted from a TestCase subclass.
55     */
56    static public void run(Class testClass) {
57        run(new TestSuite(testClass));
58    }
59
60    /**
61     * Runs a single test and collects its results.
62     * This method can be used to start a test run
63     * from your program.
64     * <pre>
65     * public static void main (String[] args) {
66     *     test.textui.TestRunner.run(suite());
67     * }
68     * </pre>
69     */
70    static public TestResult run(Test test) {
71        TestRunner runner= new TestRunner();
72        return runner.doRun(test);
73    }
74
75    /**
76     * Runs a single test and waits until the user
77     * types RETURN.
78     */
79    static public void runAndWait(Test suite) {
80        TestRunner aTestRunner= new TestRunner();
81        aTestRunner.doRun(suite, true);
82    }
83
84    /**
85     * Always use the StandardTestSuiteLoader. Overridden from
86     * BaseTestRunner.
87     */
88    public TestSuiteLoader getLoader() {
89        return new StandardTestSuiteLoader();
90    }
91
92    public void testFailed(int status, Test test, Throwable t) {
93    }
94
95    public void testStarted(String testName) {
96    }
97
98    public void testEnded(String testName) {
99    }
100
101    /**
102     * Creates the TestResult to be used for the test run.
103     */
104    protected TestResult createTestResult() {
105        return new TestResult();
106    }
107
108    public TestResult doRun(Test test) {
109        return doRun(test, false);
110    }
111
112    public TestResult doRun(Test suite, boolean wait) {
113        TestResult result= createTestResult();
114        result.addListener(fPrinter);
115        long startTime= System.currentTimeMillis();
116        suite.run(result);
117        long endTime= System.currentTimeMillis();
118        long runTime= endTime-startTime;
119        fPrinter.print(result, runTime);
120
121        pause(wait);
122        return result;
123    }
124
125    protected void pause(boolean wait) {
126        if (!wait) return;
127        fPrinter.printWaitPrompt();
128        try {
129            System.in.read();
130        }
131        catch(Exception e) {
132        }
133    }
134
135    public static void main(String args[]) {
136        TestRunner aTestRunner= new TestRunner();
137        try {
138            TestResult r= aTestRunner.start(args);
139            if (!r.wasSuccessful())
140                System.exit(FAILURE_EXIT);
141            System.exit(SUCCESS_EXIT);
142        } catch(Exception e) {
143            System.err.println(e.getMessage());
144            System.exit(EXCEPTION_EXIT);
145        }
146    }
147
148    /**
149     * Starts a test run. Analyzes the command line arguments
150     * and runs the given test suite.
151     */
152    protected TestResult start(String args[]) throws Exception {
153        String testCase= "";
154        boolean wait= false;
155
156        for (int i= 0; i < args.length; i++) {
157            if (args[i].equals("-wait"))
158                wait= true;
159            else if (args[i].equals("-c"))
160                testCase= extractClassName(args[++i]);
161            else if (args[i].equals("-v"))
162                System.err.println("JUnit "+Version.id()+" by Kent Beck and Erich Gamma");
163            else
164                testCase= args[i];
165        }
166
167        if (testCase.equals(""))
168            throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class");
169
170        try {
171            Test suite= getTest(testCase);
172            return doRun(suite, wait);
173        }
174        catch(Exception e) {
175            throw new Exception("Could not create and run test suite: "+e);
176        }
177    }
178
179    protected void runFailed(String message) {
180        System.err.println(message);
181        System.exit(FAILURE_EXIT);
182    }
183
184    public void setPrinter(ResultPrinter printer) {
185        fPrinter= printer;
186    }
187
188
189}
190
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger testFailed code on LambdaTest Cloud Grid

Execute automation tests with testFailed on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)