如何只运行一次测试?

时间:2018-01-15 16:02:56

标签: testng

例如,我有下一组测试。三组。我想运行下一组组:

  • SMOKE
  • FEATURE_1
  • FEATURE_2

而且我不想两次运行测试,例如,smokeAndFeature1测试。

我可以仅使用testNG归档此目标吗?

从我这边看,我看到了几个解决方案:

  1. 使用java创建测试套件,但不使用xml
  2. 使用god对象存储有关已执行测试用例的元数据,如果在
  3. 之前执行测试,则在侦听器中跳过测试
  4. 以某种方式在<method-selector>标记

    中使用BeanShell脚本

    public class GroupsTest {

    private static final String SMOKE = "SMOKE";
    private static final String FEATURE_1 = "FEATURE_1";
    private static final String FEATURE_2 = "FEATURE_2";;
    
    @Test(groups = {SMOKE})
    public void smokeOnly(){
        System.out.println("smokeOnly");
    }
    
    @Test(groups = {FEATURE_1})
    public void feature1Only() {
        System.out.println("feature1Only");
    }
    
    @Test(groups = {FEATURE_2})
    public void feature2Only() {
        System.out.println("feature2Only");
    }
    
    @Test(groups = {FEATURE_1, SMOKE})
    public void smokeAndFeature1() {
        System.out.println("smokeAndFeature1");
    }
    
    @Test(groups = {FEATURE_2, SMOKE})
    public void smokeAndFeature2() {
        System.out.println("smokeAndFeature2");
    }
    
    @Test(groups = {FEATURE_2, FEATURE_1})
    public void feature1AndFeature2() {
        System.out.println("feature1AndFeature2");
    }
    
    @Test(groups = {SMOKE, FEATURE_2, FEATURE_1})
    public void smokeFeature1AndFeature2() {
        System.out.println("smokeFeature1AndFeature2");
    }
    

    }

1 个答案:

答案 0 :(得分:0)

最后,我发现了我想做的事情。我想摆脱使用群体的想法。它只需要运行一个测试版。

例如,如果我有4台设备首先运行一台设备将仅启动第1,第5,第9等测试。第二,第六,第十等设备

我使用了testng提供的方法选择器机制。

我的方法选择器实现:

import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;

import java.util.List;

public class ParallelExecutionMethodSelector implements IMethodSelector {
    private int counter = 1;
    private int part    = 1;

    @Override
    public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {

        if (isTestMethod) {
            final int testIndex   = 1 + method.getXmlTest().getIndex();
            final int partVal     = part;
            final int testNumber  = method.getXmlTest().getSuite().getTests().size();

            // split a list of test in chunks
            if (counter / partVal == testNumber ) {
                part++;
            }

            // pick up only a test with the value id as current test index
            boolean include = (counter - testNumber * (partVal - 1) == testIndex);

            // To disable the next method selector class
            context.setStopped(true);

            counter++;

            return include;
        }

        return false;
    }

    @Override
    public void setTestMethods(List<ITestNGMethod> testMethods) {
    }
}

我将它用于下一个testng xml配置

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Debug" parallel="tests" thread-count="4">
    <method-selectors>
        <method-selector>
            <selector-class name="com.example.ParallelExecutionMethodSelector" />
        </method-selector>
    </method-selectors>

    <test name="Debug Thread-1" enabled="true">
        <parameter name="deviceName" value="22456BDE5D448B047D8A4"/>

        <packages>
            <package name="com.mobile.tests.*" />
        </packages>
    </test>

    <test name="Debug Thread-2" enabled="true">
        <parameter name="deviceName" value="2F212D84C28C8AED24F0E"/>

        <packages>
            <package name="com.mobile.tests.*" />
        </packages>
    </test>

    <test name="Debug Thread-3" enabled="true">
        <parameter name="deviceName" value="A91730017660D2215CA6A"/>

        <packages>
            <package name="com.mobile.tests.*" />
        </packages>
    </test>

    <test name="Debug Thread-4" enabled="true">
        <parameter name="deviceName" value="3AFEC04596F29DA3A7948"/>

        <packages>
            <package name="com.mobile.tests.*" />
        </packages>
    </test>
</suite>