Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to integrate Jbehave with testNG?

Currently We're using TDD and planing to migrate into BDD using Jbehave. I just done some google search and not able to find any site for testNG with Jbehave. I just went through the Jbehave official site where I understood that possible to integrate the jbehave library with any type of unit testing tool like TestNg, Junit. But I didn't find any sample code that how to actually do it. I'm expecting the following steps from some experts:

  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?

I'm really not expecting any practical example or working code. I just wanna the understand the overview journey, and some inputs to achieve this task.

Its really helpful If some one share link, and very basic code which makes me more clear on that.

The following things so far I tried :

Feature file:

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

Test step class file:

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();
        }

    }

But stuck to create a test runner class file using TestNg. I'm not sure How to start.

Can somebody help me to create a test runner class file which will execute the above code.

I read some of the materials but I don't have sufficient time to read all those, and implement It would be highly thankful If some one help me out.

like image 504
SKumar Avatar asked Aug 28 '15 09:08

SKumar


People also ask

Can we implement BDD using JBehave?

JBehave is a Java framework for writing and running BDD tests. It has many moving parts that make it easy to fit into your workflow and tools. You can write your tests with the DSL in the example above or Gherkin syntax.

Can we use serenity with TestNG?

Serenity aims to make it easy and quick to write well-structured, maintainable automated acceptance criteria, using best BDD libraries like Jbehave/Cucumber or conventional testing library likes Junit, TestNG.

What is difference between TestNG and JUnit?

JUnit does not include any dependency tests. TestNG includes dependency tests. JUnit is a framework that is open-source and may be used to write and trigger tests. TestNG is a Java-based framework that provides an improved way to run tests.


1 Answers

  1. From 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.

For the Embedder-based methods, there's documentation here.

  1. You probably want to use JBehave's annotations rather than TestNG's, as you'll be running stories and scenarios rather than classes and suites. As you can see, one story (of several scenarios) is treated as one test, which makes sense if you think about things like background.

  2. Since TestNG treats one story as one test, I can't see any reason why it wouldn't be picked up. Give it a try.

  3. For test reports, see JBehave's documentation again. It allows the injection of custom reporters.

Having said all that, don't migrate away from TDD. The ideal is to have a few high-level scenarios that traverse the whole system, more integration tests than that, and an absolute ton of unit tests.

For instance, if you have a form to validate, just put a couple of examples that show how the user is guided to fill in the form. Check the rest of the validation by creating unit tests for the validation class. (You can still think of them as examples, and put the Given / When / Then in comments, but using JBehave is overkill for tests with a technical audience.)

See the testing pyramid for more information, and avoid the testing ice-cream cone.

like image 139
Lunivore Avatar answered Oct 23 '22 04:10

Lunivore