How to use countTestCases method of junit.framework.TestCase class

Best junit code snippet using junit.framework.TestCase.countTestCases

Run junit automation tests on LambdaTest cloud grid

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

copy
1package junit.tests.framework;
2
3import junit.framework.Protectable;
4import junit.framework.Test;
5import junit.framework.TestCase;
6import junit.framework.TestResult;
7
8/**
9 * Test an implementor of junit.framework.Test other than TestCase or TestSuite
10 */
11public class TestImplementorTest extends TestCase {
12    public static class DoubleTestCase implements Test {
13        private TestCase fTestCase;
14
15        public DoubleTestCase(TestCase testCase) {
16            fTestCase = testCase;
17        }
18
19        public int countTestCases() {
20            return 2;
21        }
22
23        public void run(TestResult result) {
24            result.startTest(this);
25            Protectable p = new Protectable() {
26                public void protect() throws Throwable {
27                    fTestCase.runBare();
28                    fTestCase.runBare();
29                }
30            };
31            result.runProtected(this, p);
32            result.endTest(this);
33        }
34    }
35
36    private DoubleTestCase fTest;
37
38    public TestImplementorTest() {
39        TestCase testCase = new TestCase() {
40            @Override
41            public void runTest() {
42            }
43        };
44        fTest = new DoubleTestCase(testCase);
45    }
46
47    public void testSuccessfulRun() {
48        TestResult result = new TestResult();
49        fTest.run(result);
50        assertEquals(fTest.countTestCases(), result.runCount());
51        assertEquals(0, result.errorCount());
52        assertEquals(0, result.failureCount());
53    }
54}
55
Full Screen
copy
1package junit.tests.extensions;
2
3import junit.extensions.RepeatedTest;
4import junit.framework.Test;
5import junit.framework.TestCase;
6import junit.framework.TestResult;
7import junit.framework.TestSuite;
8
9/**
10 * Testing the RepeatedTest support.
11 */
12public class RepeatedTestTest extends TestCase {
13    private TestSuite fSuite;
14
15    public static class SuccessTest extends TestCase {
16
17        @Override
18        public void runTest() {
19        }
20    }
21
22    public RepeatedTestTest(String name) {
23        super(name);
24        fSuite = new TestSuite();
25        fSuite.addTest(new SuccessTest());
26        fSuite.addTest(new SuccessTest());
27    }
28
29    public void testRepeatedOnce() {
30        Test test = new RepeatedTest(fSuite, 1);
31        assertEquals(2, test.countTestCases());
32        TestResult result = new TestResult();
33        test.run(result);
34        assertEquals(2, result.runCount());
35    }
36
37    public void testRepeatedMoreThanOnce() {
38        Test test = new RepeatedTest(fSuite, 3);
39        assertEquals(6, test.countTestCases());
40        TestResult result = new TestResult();
41        test.run(result);
42        assertEquals(6, result.runCount());
43    }
44
45    public void testRepeatedZero() {
46        Test test = new RepeatedTest(fSuite, 0);
47        assertEquals(0, test.countTestCases());
48        TestResult result = new TestResult();
49        test.run(result);
50        assertEquals(0, result.runCount());
51    }
52
53    public void testRepeatedNegative() {
54        try {
55            new RepeatedTest(fSuite, -1);
56        } catch (IllegalArgumentException e) {
57            assertTrue(e.getMessage().contains(">="));
58            return;
59        }
60        fail("Should throw an IllegalArgumentException");
61    }
62}
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 countTestCases code on LambdaTest Cloud Grid

Execute automation tests with countTestCases 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)