首页 > 解决方案 > 空手道有没有办法使用 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 内失败的场景,然后是另一个失败的场景:一些测试,这是我运行的测试。

如果场景中的某个步骤失败,关于如何触发单个事件的任何想法:一些测试?

标签: karatejunit5

解决方案


空手道 JUnit5 支持基于DynamicNode并且已知不受通常的生命周期约束。它旨在使开发人员在 IntelliJ 和 Eclipse 中获得更好的体验,仅此而已。

也许你应该看看空手道ExecutionHook


推荐阅读