在 Allure 报告中,如何在 TestNG 的参数化测试中为每个数据集提供不同的严重性?

In Allure reports, how to provide distinct severity to each data set in parameterized test in TestNG?

我知道我们可以在下面提到诱惑报告测试的严重性:

@Severity(SeverityLevel.CRITICAL)
@Test(dataProvider = "TH_DataProvider",enabled=true)
public void M01_createRelease(String sNo, String te....)

但上述配置的问题在于,对于所有测试数据组合,SeverityLevel 都设置为 CRITICAL。但理想情况下,SeverityLevel 会因数据提供者提供的每个数据集而异。是否可以为 TestNG 数据提供者提供的每个数据集提及不同的 SeverityLevel?

您始终可以通过编程方式设置严重性:

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import ru.yandex.qatools.allure.Allure;
import ru.yandex.qatools.allure.events.TestCaseEvent;
import ru.yandex.qatools.allure.model.SeverityLevel;
import ru.yandex.qatools.allure.model.TestCaseResult;

import static org.testng.Assert.assertTrue;
import static ru.yandex.qatools.allure.AllureUtils.createSeverityLabel;

public class ParameterizedTestngTest {

    @DataProvider(name = "TH_DataProvider")
    public static Object[][] dataProvider() {
        return new Object[][] {{"Minor", "Test data 1"}, {"Major", "Test data 2"}, {"Blocker", "Test data 3"}};
    }

    // @Severity(SeverityLevel.TRIVIAL) // overwriting annotation is also possible, please uncomment code in event class
    @Test(dataProvider = "TH_DataProvider", enabled = true)
    public void M01_createRelease(String sNo, String te) {
        SeverityLevel severity = SeverityLevel.TRIVIAL;

        // overwriting default severity
        if (sNo.equals("Minor")) {
            severity = SeverityLevel.MINOR;
        } else if (sNo.equals("Blocker")) {
            severity = SeverityLevel.BLOCKER;
        }
        Allure.LIFECYCLE.fire(new SetSeverityEvent(severity));

        // test
        assertTrue(true);
    }

    public static class SetSeverityEvent implements TestCaseEvent{

        private SeverityLevel severity;

        public SetSeverityEvent(SeverityLevel severity) {
            this.severity = severity;
        }

        @Override
        public void process(TestCaseResult context) {
            context.withSeverity(severity);

            // you can additionally remove severity label im case you use @Severity annotation as your default severity
//            context.setLabels(Lists.newArrayList(Collections2.filter(context.getLabels(), new Predicate<Label>() {
//                @Override
//                public boolean apply(Label label) {
//                    return !label.getName().equals(LabelName.SEVERITY);
//                }
//            })));
        }
    }

}