我的问题主要是要知道如何在 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";
}