如何从空手道运行junit测试?

时间:2018-09-25 02:06:40

标签: karate

可以从空手道运行junit测试吗?

作为我们项目的一部分,我们需要添加开发人员编写的junit测试以及空手道测试(由QA编写)

寻找一种从空手道中调用junit测试的方法。这样很容易将Dev和QA测试保持在一起。

如果答案为是,请说明示例

1 个答案:

答案 0 :(得分:0)

您可以通过Junit调用测试。

import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import com.intuit.karate.cucumber.CucumberRunner;
import com.intuit.karate.cucumber.KarateStats;
import cucumber.api.CucumberOptions;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;

@CucumberOptions(features = {"classpath:features"}, tags = {"@sanity"})
public class RunnerTest {
  @Test
  public void testParallel() {
    String karateOutputPath = "target/cucumber-html-reports";
    KarateStats stats = CucumberRunner.parallel(getClass(), 1, karateOutputPath);
    generateReport(karateOutputPath);
    assertTrue("there are scenario failures", stats.getFailCount() == 0);
  }

  private static void generateReport(String karateOutputPath) {
    Collection<File> jsonFiles =
        FileUtils.listFiles(new File(karateOutputPath), new String[] {"json"}, true);
    List<String> jsonPaths = new ArrayList(jsonFiles.size());
    jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
    Configuration config = new Configuration(new File("target"), "demo");
    ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
    reportBuilder.generateReports();
  }
}

检查@sanity标签应该是功能文件的一部分,并且应该是第一行