1

我的问题主要是要知道如何在 Cucumber 场景运行后填充 TestRail 结果。我正在尝试将我的 JUnit 测试运行的结果设置在现有的 TestRail 运行上。根据这个项目,我有 APIClient 和 APIException 。然后我创建了这个 JUnit 类,也复制了同一个项目。作为第一次使用 Cucumber 和 JUnit,现在不确定如何进行。如果有帮助,我们的项目还有一个 Hooks 类和一个 MainRunner 吗?

public class Hooks {

public static WebDriver driver;

@Before
public void initializeTest() {

    System.out.println("Testing whether it starts before every scenario");

    driver = DriverFactory.startDriver();
}

}


import java.io.File;
@RunWith(Cucumber.class)
@CucumberOptions(
    features = {"src/test/java/clinical_noting/feature_files/"},
    glue = {"clinical_noting.steps", "clinical_noting.runner"},
    monochrome = true,
    tags = {"@current"},
    plugin = {"pretty", "html:target/cucumber", 
"json:target/cucumber.json", 
"com.cucumber.listener.ExtentCucumberFormatter:target/cucumber- 
reports/report.html"}
)

public class MainRunner {

@AfterClass
public static void writeExtentReport() {
    Reporter.loadXMLConfig(new File(FileReaderManager.getInstance().getConfigReader().getReportConfigPath())) 
;
}

}

谢谢您的帮助。

更新

单独运行 JUnit 测试时更新 TestRail。在 Cucumber 场景运行后仍然不确定如何操作?这就是它现在的工作方式:

public class JUnitProject {
private static APIClient client = null;
private static Long runId = 3491l;
private static String caseId = "";
private static int FAIL_STATE = 5;
private static int SUCCESS_STATE = 1;
private static String comment = "";
@Rule
public TestName testName = new TestName();

@BeforeClass
public static void setUp() {
    //Login to API
    client = testRailApiClient();
}

@Before
public void beforeTest() throws NoSuchMethodException {
    Method m = JUnitProject.class.getMethod(testName.getMethodName());
    if (m.isAnnotationPresent(TestRails.class)) {
        TestRails ta = m.getAnnotation(TestRails.class);
        caseId = ta.id();
    }
}

@TestRails(id = "430605")
@Test
public void validLogin() {
    comment = "another comment";
    Assert.assertTrue(true);
}

@Rule
public final TestRule watchman = new TestWatcher() {
    Map data = new HashMap();

    @Override
    public Statement apply(Statement base, Description description) {
        return super.apply(base, description);
    }

    @Override
    protected void succeeded(Description description) {
        data.put("status_id", SUCCESS_STATE);
    }

    // This method gets invoked if the test fails for any reason:
    @Override
    protected void failed(Throwable e, Description description) {
        data.put("status_id", FAIL_STATE);
    }

    // This method gets called when the test finishes, regardless of status
    // If the test fails, this will be called after the method above
    @Override
    protected void finished(Description description) {
        try {
            data.put("comment", comment);
            client.sendPost("add_result_for_case/" + runId + "/" + caseId, data);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (APIException e) {
            e.printStackTrace();
        }
    };
};
}

和注释

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //on method level
public @interface TestRails {

String id() default "none";
}
4

1 回答 1

0

现在工作。必须在 before 方法中添加场景参数并从那里进行 TestRail 连接。

@regressionM1 @TestRails(430605)
Scenario: Verify the user can launch the application
Given I am on the "QA-M1" Clinical Noting application
Then I should be taken to the clinical noting page

public class Hooks {

private static APIClient client = null;
private static Long runId = 3491l;
private static String caseId = "";
private static int FAIL_STATE = 5;
private static int SUCCESS_STATE = 1;
private static String SUCCESS_COMMENT = "This test passed with Selenium";
private static String FAILED_COMMENT = "This test failed with Selenium";
@Rule
public TestName testName = new TestName();

public static WebDriver driver;

@Before
public void initializeTest() {
    client = testRailApiClient();

    System.out.println("Testing whether it starts before every scenario");

    driver = DriverFactory.startDriver();
}

@After()
public void tearDown(Scenario scenario) {
    String caseIdSplit = "";

    for (String s : scenario.getSourceTagNames()) {
        if (s.contains("TestRail")) {
            caseIdSplit = s.substring(11, 17); // Hardcoded for now as all the ids have 6 characters
            System.out.println("Testing whether the browser closes after every scenario" + caseIdSplit);
        }
    }

    caseId = caseIdSplit;
    Map data = new HashMap();

    if (!scenario.isFailed()) {
        data.put("status_id", SUCCESS_STATE);
        data.put("comment", SUCCESS_COMMENT);

    } else if (scenario.isFailed()) {

        data.put("status_id", FAIL_STATE);
        data.put("comment", SUCCESS_COMMENT);
    }

    try {
        client.sendPost("add_result_for_case/" + runId + "/" + caseId, data);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (APIException e) {
        e.printStackTrace();
    }
}

}

更新在这里 写了一篇关于这个的帖子

于 2019-11-15T14:13:28.503 回答