是否可以将 Jbehave 与 testNG 集成?

Is it possible to integrate Jbehave with testNG?

目前我们正在使用 TDD,并计划使用 Jbehave 迁移到 BDD。我刚刚进行了一些 google 搜索,但无法找到任何使用 Jbehave 进行 testNG 的站点。我刚刚浏览了 Jbehave 官方网站,在那里我了解到可以将 jbehave 库与任何类型的单元测试工具(如 TestNg、Junit)集成。但是我没有找到任何示例代码来说明如何实际执行此操作。 我期待一些专家采取以下步骤:

  1. How to create a simple java file with Jbehave+TestNG.
  2. Is it possible to use all the features of TestNG after I implemented with Jbehave(Like, annotations. BeforeClass, afterClass, BeforeSuite, AfterSuite)

  3. How to run the Jbehave feature file/class through TestNG.xml file.

  4. How to integrate custom test report in Jbehave?

我真的不期待任何实际示例或工作代码。我只想了解概览过程,以及完成此任务的一些输入。

如果有人分享 link,它真的很有帮助,而且非常基本的代码让我更清楚这一点。

到目前为止我尝试了以下内容:

专题文件:

Scenario: Check the google search engine
Given : Open the google home page www.google.com
When : Enter test automation in search box
Then : Proper result should be displayed in results page

测试步骤class文件:

public class GoogleSearchEngine_Steps {

    public static WebDriver driver;

    @Given("Open the google home page $url")
    public static void openUrl(String url) throws Exception {
        try {

            driver = new FirefoxDriver();
            driver.get(url);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @When("Enter $searchKeyword in search box")
    public static void searchKeyword(String searchKeyword) throws Exception {
        try {

            driver.findElement(By.xpath(".//*[@id='gs_htif0']")).sendKeys(searchKeyword);
            driver.findElement(By.xpath(".//*[@id='tsf']/input[1]")).click();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Then("Proper result should be displayed in results page")
    public static void result() throws Exception {
        try {

            driver.quit();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

但坚持使用 TestNg 创建测试运行器 class 文件。我不确定如何开始。

有人可以帮我创建一个测试运行器 class 文件来执行上面的代码。

我阅读了一些资料,但我没有足够的时间阅读所有这些资料,并实施 如果有人帮助我,将不胜感激。

  1. 来自 JBehave's FAQ:

A quick fix to run with TestNG is simply to annotate the run() method in your root JUnitStory/Stories class with the TestNG @Test annotation:

public class YourStory extends JUnitStory/Stories {

    @org.testng.annotations.Test 
    public void run() throws Throwable {
        super.run();
    }
}

Note that this is just one way to run stories and if you use other Embedder-based methods you'll need to change the test annotation used correspondingly.

对于基于嵌入器的方法,有文档 here

  1. 您可能想要使用 JBehave's annotations 而不是 TestNG,因为您将成为 运行 故事和场景而不是 class 系列和套件。如您所见,一个故事(多个场景)被视为一个测试,如果您考虑背景之类的事情,这是有道理的。

  2. 由于 TestNG 将一个故事视为一个测试,我看不出有任何理由不接受它。试试吧。

  3. 测试报告,再看JBehave's documentation。它允许注入自定义记者。

说了这么多,不要离开 TDD。理想的是有一些遍历整个系统的高级场景,比这更多的集成测试,以及绝对大量的单元测试。

例如,如果您有一个要验证的表单,只需举几个示例来说明如何引导用户填写表单。通过为验证 class 创建单元测试来检查验证的其余部分。 (您仍然可以将它们视为示例,并将 Given / When / Then 放在注释中,但是使用 JBehave 对于技术受众的测试来说太过分了。)

参见testing pyramid for more information, and avoid the testing ice-cream cone