BDD Jbehave在执行Pending时的故事

时间:2018-03-07 12:46:30

标签: maven bdd jbehave

最近我开始使用JBehave开发BDD。     到目前为止,如果我使用maven运行,我的maven项目正在成功构建。然后它进入故事文件,但随后它不再进一步。

我尝试使用junit运行但是我得到了相同的结果..

我认为我的问题在于执行程序文件。     我搜索了许多网站甚至Jbehave.org和许多stackoverflow查询..但是徒劳无功     帮助我解决这个问题...如果您需要任何其他信息,请告诉我

I spent so much time rectifying this.But couldn't able to find the solution.
Here is my runner file..
package runnerFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.junit.JUnitStory;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.ScanningStepsFactory;
import org.jbehave.core.steps.Steps;


public class TestRunner extends JUnitStories{ 
    @Override
    public Configuration configuration() { 
        return new MostUsefulConfiguration()                
                .useStoryLoader(
                        new LoadFromClasspath(this.getClass().getClassLoader()))
                .useStoryReporterBuilder(
                        new StoryReporterBuilder()
                            .withDefaultFormats()
                            .withFormats(Format.HTML, Format.CONSOLE)
                            .withRelativeDirectory("jbehave-report")
                );
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
      //  ArrayList<Object> stepFileList = new ArrayList<Object>();
        ArrayList<Steps> stepFileList = new ArrayList<Steps>();
       stepFileList.add(new Steps(configuration()));
       return new InstanceStepsFactory(configuration(), stepFileList);
        //return new ScanningStepsFactory(configuration(), "org.jbehave.examples.core.steps", "my.other.steps"`enter code here` ).matchingNames(".*Steps").notMatchingNames(".*SkipSteps");     
    }

    @Override
    protected List<String> storyPaths() {
       return new StoryFinder().
               findPaths(CodeLocations.codeLocationFromClass(
                       this.getClass()), 
                       Arrays.asList("**/TC_2.story"), 
                       Arrays.asList(""));
}
}




I kept my story file inside src/test/resources . and step definition inside src/test/java

****story:****

**src/test/resources**

Narrative:
In order to communicate effectively to the business some functionality
As a development team
I want to use Behaviour-Driven Development

Scenario:  A scenario is a collection of executable steps of different type
Given I launch the url
When I login with username <Username> and password <Password>
Then I should see the homepage

Examples:
|Username|Password|
|test@gmail.com|test1234|

**stepDefinition**

**src/test/java:**


package definition;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import pages.Homepage_Pages;

public class HomePage {

    Homepage_Pages home;

    @Given("I launch the url")
    public void url()
    {
        home.launchUrl();
    }

    @When("I login with username <Username> and password <Password>")
    public void login(@Named("Username") String Username, @Named("Password") String Password)
    {
        System.out.println(Username);
    }

    @Then("I should see the homepage")
    public void homePageVerification()
    {
        System.out.println("Heello");
    }
}

Maven控制台:

Maven COnsole

1 个答案:

答案 0 :(得分:0)

尝试以下代码,这是一个简单的简单testrunner,它没有任何花哨的功能,只是运行在主文件夹的子文件夹中找到的所有故事,并包含定义步骤文件位置中的所有步骤类。我原来有很多硬编码的东西,但我把它们改成了最终的字符串,所以它应该很容易替换你的情况并运行这个文件。显然,使用放置步骤文件的任何包文件夹更改“com.yourpackage.steps”。希望这会有所帮助。

package testrunner;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.embedder.EmbedderControls;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.CrossReference;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.junit.runner.RunWith;

import de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner;

@RunWith(JUnitReportingRunner.class)
public class TestRunner extends JUnitStories {

    private Configuration configuration;

    public TestRunner() {

        super();

        CrossReference crossReference = new CrossReference();

        configuration = new MostUsefulConfiguration();

        configuration.useStoryReporterBuilder(
                new StoryReporterBuilder().withFormats(Format.HTML, Format.STATS, Format.CONSOLE)
                .withCodeLocation(CodeLocations.codeLocationFromPath("target/."))
                .withCrossReference(crossReference));

        EmbedderControls embedderControls = configuredEmbedder().embedderControls();

        embedderControls.doBatch(false);
        embedderControls.doGenerateViewAfterStories(true);
        embedderControls.doSkip(false);
        embedderControls.doVerboseFailures(true);
        embedderControls.doVerboseFiltering(true);
        embedderControls.useThreads(1);
        embedderControls.useStoryTimeouts("1800");

    }

    @Override
    protected List<String> storyPaths()
    {
        return new StoryFinder().findPaths(CodeLocations.codeLocationFromClass(this.getClass()), "**/*.story", "");
    }

    @Override
    public Configuration configuration() {
        return configuration;
    }

    @Override
    public InjectableStepsFactory stepsFactory() {

        final String stepsPackage = "com.yourpackage.steps";
        final String stepsLoc = "src/test/java/" + stepsPackage.replace(".", "/");

        List<Object> stepList = new ArrayList<Object>();

        File steps = new File(stepsLoc);
        File[] fileList = steps.listFiles();

        int size = fileList.length;

        for (int i = 0; i < size; i++) {

            if (fileList[i].isFile()) {                                     // also returns folders (directories)
                String value = fileList[i].getName().replace(".java", "");  // strip extensions
                if (!value.toLowerCase().contains("testrunner")) {          // ignore testrunner itself
                    try {
                        Object stepObject = Class.forName((stepsPackage + "." + value)).newInstance();
                        stepList.add(stepObject);
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return new InstanceStepsFactory(configuration(), stepList);
    }

}