karate - 空手道有没有办法使用 Junit5 中的 TestExecutionListener?
问题描述
我正在尝试使用https://junit.org/junit5/docs/current/user-guide/#launcher-api-listeners-custom,创建了我自己的实现 TestExecutionListener 的侦听器:
public class JunitTestListener implements TestExecutionListener {
public static final Logger logger = LoggerFactory.getLogger(JunitTestListener.class);
/**
* Called when the execution of the {@link TestPlan} has finished,
* <em>after</em> all tests have been executed.
*
* @param testPlan describes the tree of tests that have been executed
*/
@Override
public void testPlanExecutionFinished(TestPlan testPlan) {
logger.info("Mutzu was here");
}
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
logger.info("Mutzu was here");
}
}
我创建了 META-INF/services/org.junit.platform.launcher.TestExecutionListener 并在那里添加了自定义侦听器类,但是在使用空手道运行时不会调用它:
import com.intuit.karate.junit5.Karate;
class E2ETest {
// this will run all *.feature files that exist in sub-directories
// see https://github.com/intuit/karate#naming-conventions
@Karate.Test
Karate testAll() {
return Karate.run().relativeTo(getClass());
}
}
关于如何使它工作的任何想法?
谢谢!
似乎有了这个 ExecutionHook,我可以访问之后/之前的步骤/场景/功能,但是我无法控制由其他场景组成的场景的结果。我不可能在 ExecutionHook 中知道“测试”的结果是什么,在我的接受中测试意味着定义和组合其他场景的场景。例如,如果我有:
Background:
* def feature1 = read('classpath:create_schema.feature')
* def feature2 = read('classpath:create_dataset.feature')
* def feature3 = read('classpath:create_mergepolicy.feature')
Scenario: Some test
Given def schema = call feature1
And def schemaId = schema.schemaId
Given def datasetArg = { schemaId: '#(schemaId)' }
And def dataset = call feature2 datasetArg
And def datasetId = dataset.datasetId
Given def mergePolicyArg = { datasetId: '#(datasetId)' }
And def mergePolicy = call feature3 mergePolicyArg
And def mergePolicyId = mergePolicy.mergePolicyId
如果在某个步骤出现故障,比如说在 feature1 内调用,那么使用 ExecutionHook 我会收到 2 次失败事件,一次是在 feature1 内失败的场景,然后是另一个失败的场景:一些测试,这是我运行的测试。
如果场景中的某个步骤失败,关于如何触发单个事件的任何想法:一些测试?
解决方案
空手道 JUnit5 支持基于DynamicNode
并且已知不受通常的生命周期约束。它旨在使开发人员在 IntelliJ 和 Eclipse 中获得更好的体验,仅此而已。
也许你应该看看空手道ExecutionHook。
推荐阅读
- c# - C#使用Socket向所有客户端发送消息
- javascript - Javascript + Google Map:如何获取地理位置然后分配地图中心?
- html - HTML 元素根据窗口大小改变位置
- c++ - Floodfill 未正确着色指定参数
- java - 从 n 行文本文件中查找等价类
- cakephp - 如何在 cakePHP 3 中使用外键删除值?
- jestjs - 如何使用 jest 和 babel v7 避免“TypeError:无法设置属性”错误?
- c - 如何在格式错误的输入中正确使用带有循环的 sscanf?
- android - Gradle设置java类的outputDir
- r - 计算有条件的记录数