简体   繁体   English

使用黄瓜从Maven的Main运行Junit测试

[英]Running Junit Tests from Main in Maven with Cucumber

I have a simple test runner class in a sub directory: src/test/java 我在子目录中有一个简单的测试运行器类:src / test / java

@RunWith(Cucumber.class)
//@Feature("my/package/**/*.feature")
@CucumberOptions(

  features= {"src/test/java/multiTest/Login_Logout.feature"}
, glue={"stepDefs"}
, monochrome = true
, plugin = {"pretty"}
)

public class TestRunner {
public static void main(String[] args) throws Exception {    
    System.out.println("This is a test");
    JUnitCore.main("multiTest.TestRunner");    
}
}

From my main file: src/main/java I would like to call this TestRunner class. 从我的主文件:src / main / java我想调用这个TestRunner类。 Eventually this will be configured to take user input from the main thread and pass it into the testing thread. 最终,这将被配置为从主线程获取用户输入并将其传递到测试线程。 However the last line in this class is throwing compilation errors. 但是这个类的最后一行是抛出编译错误。

public static void main(String[] args) throws Exception {
     JFrame frame = new JFrame("QA for EDI");
    // Setting the width and height of frame
    frame.setSize(500, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    /* Creating panel. This is same as a div tag in HTML
     * We can create several panels and add them to specific 
     * positions in a JFrame. Inside panels we can add text 
     * fields, buttons and other components.
     */
    JPanel panel = new JPanel();    
    // adding panel to frame
    frame.add(panel, BorderLayout.CENTER);
    TestRunner.run(TestRunner.class);
}

Is there a way to achieve what I am trying to do? 有没有办法实现我想做的事情?

TestRunner does not have a static run() method thus you get the error. TestRunner没有静态run()方法,因此您得到错误。 The run() belongs to the Thread class. run()属于Thread类。

What are you trying to achieve by this setup? 您想通过此设置实现什么目标? If you want to pass any data put it in the feature file directly. 如果要传递任何数据,请将其直接放入要素文件中。 That is the whole point of scenarios in Cucumber. 这就是Cucumber中的情景的全部要点。 Put in all the different things you want to test as separate scenarios. 将您想要测试的所有不同内容作为单独的方案放入。

Anyways to use the TestRunner class to do anything Cucumber specific you need to use the Cucumber TestRunner class which is mentioned in the RunWith annotation. 无论如何要使用TestRunner类来执行任何特定的Cucumber,你需要使用RunWith注释中提到的Cucumber TestRunner类。 And the main method in the TestRunner will never be called by Cucumber. 而且,Cucumber永远不会调用TestRunner的主要方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM