TestNG: How can I run same test case multiple times?

后端 未结 8 1069
你的背包
你的背包 2020-12-30 20:22

I want to run a test case multiple times. Is that configurable in the testng.xml? If I add a loop in the test method, then the results of each run will not be a

相关标签:
8条回答
  • 2020-12-30 20:57

    You can add multiple tests in testngSuite and execute. Under all tests the classes names should be same inorder to execute same script multiple number of times.

    0 讨论(0)
  • 2020-12-30 21:00

    If you don't mind using Sprint, you can create this class:

    package somePackage;
    
    import org.junit.Ignore;
    import org.junit.runner.Description;
    import org.junit.runner.notification.RunNotifier;
    import org.junit.runners.BlockJUnit4ClassRunner;
    import org.junit.runners.model.FrameworkMethod;
    import org.junit.runners.model.InitializationError;
    import org.junit.runners.model.Statement;
    import org.springframework.test.annotation.Repeat;
    
    public class ExtendedRunner extends BlockJUnit4ClassRunner {
    
        public ExtendedRunner(Class<?> klass) throws InitializationError {
            super(klass);
        }
    
        @Override
        protected Description describeChild(FrameworkMethod method) {
            if (method.getAnnotation(Repeat.class) != null
                    && method.getAnnotation(Ignore.class) == null) {
                return describeRepeatTest(method);
            }
            return super.describeChild(method);
        }
    
        private Description describeRepeatTest(FrameworkMethod method) {
            int times = method.getAnnotation(Repeat.class).value();
    
            Description description = Description.createSuiteDescription(
                testName(method) + " [" + times + " times]",
                method.getAnnotations());
    
            for (int i = 1; i <= times; i++) {
                description.addChild(Description.createTestDescription(
                    getTestClass().getJavaClass(), "[" + i + "] "
                            + testName(method)));
            }
            return description;
        }
    
        @Override
        protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
            Description description = describeChild(method);
    
            if (method.getAnnotation(Repeat.class) != null
                    && method.getAnnotation(Ignore.class) == null) {
                runRepeatedly(methodBlock(method), description, notifier);
            }
            super.runChild(method, notifier);
        }
    
        private void runRepeatedly(Statement statement, Description description,
                RunNotifier notifier) {
            for (Description desc : description.getChildren()) {
                runLeaf(statement, desc, notifier);
            }
        }
    
    }
    

    Then in the actual test:

    package somePackage;
    
    import *.ExtendedRunner;
    
    import org.junit.Ignore;
    import org.junit.runner.RunWith;
    import org.springframework.test.annotation.Repeat;
    
    @Ignore
    @RunWith(ExtendedRunner.class)
    public class RepeatedTest{
        @Repeat(value N)
        public void testToBeRepeated() {
    
        }
    }
    

    Where N is the number of times you want the test to repeat.

    0 讨论(0)
提交回复
热议问题