Creating Examples for ScenarioOutline in code

Deadly 提交于 2019-12-24 11:34:24

问题


I want to dynamically create multiple examples for a ScenarioOutline in a feature file. Is it possible to do this in the @before hook somehow?

I know this is not how you're supposed to use cucumber, but how would it be possible?

I already tried accesing the Scenario in the hook, but there are no methods to get all steps and their variables/placeholders


回答1:


Cucumber doesn't encourage to have examples outside feature file.

However there are few non standard way available with cucumber to use examples outside the feature file. One of them, you can refer in grasshopper's post.

Another alternate is using gherkin with QAF which provides lots of features inbuilt data-providers including XML/CSV/JSON/EXCEL/DB. It also supports to provide example generated through code using custom data-provider. For example:

Scenario Outline: scenario with dynamic test-data
....

Examples:{"dataProvider":"dynamic-examples", "dataProviderClass":"my.project.impl.CustomExamplesProvider"}

package my.project.impl;
public class CustomExamplesProvider{

    @DataProvider(name="dynamic-examples")
    public static Object[][] dataProviderForBDD(){
        //generate and return data. 
        //This is just example with hard-coded values and you can generate and return data as per need.
        Map<Object, Object> ex1 = Maps.newHashMap();
        ex1.put("fruit", "grapes");
        ex1.put("color", "green");

        Map<Object, Object> ex2 = Maps.newHashMap();
        ex2.put("fruit", "banana");
        ex2.put("color", "yellow");

        return new Object[][] {{ex1},{ex2}} ;
    }
}



回答2:


This has been asked a couple of times before, usually as the more specific question "How can I import scenario outline examples from CSV?". You might find a workaround that works for you by researching that question, such as this answer that suggests using QAF Gherkin scenario factory, or this answer that suggest passing a CSV into the scenario, and then using the example table to index into it.

BUT, that said, defining scenarios dynamically from file is specifically listed in the Cucumber FAQ as an anti-pattern

We advise you not to use Excel or csv files to define your test cases; using Excel or csv files is considered an anti-pattern.

One of the goals of Cucumber is to have executable specifications. This means your feature files should contain just the right level of information to document the expected behaviour of the system. If your test cases are kept in separate files, how would you be able to read the documentation?

And sometimes when this question gets asked, there's a strong response from people who know the pain of living with a misused BDD tool, practically begging them not to do it.

Cucumber as a BDD tool involves a lot of overhead (writing feature files) and provides a certain value (a vibrant, team-wide understanding of how the product should work, probably). If you write feature files that don't buy you that value, you're investing all this time into an expensive, unnecessary layer of your test framework. Cucumber basically becomes a glorified test runner, and there are much cheaper ways to run your test if you don't really need the value BDD is supposed to provide.



来源:https://stackoverflow.com/questions/55535963/creating-examples-for-scenariooutline-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!