问题
I'm using Behat for BDD and using Scenario Outlines so I can easily do the same test with other data. But I got a problem with large texts. See example below:
Scenario Outline: create a thing
When I click on "New"
Then I should be at "/thing/new"
When I fill in "title" with <title>
When I fill in "description" with "description"
When I click on "save"
Then I should be at "/things"
Then I should see <title> in the list
When I click on <title>
Then I should see <title>
Then I should see <description>
Examples:
| title | description |
| "My new thing" | "a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string" |
As you can imagine this can be annoying if there are more large texts or more type of values. Is there a solution for this? For example using a variable? Which could be something like this:
$myvar = "a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string"
Scenario Outline: create a thing
When I click on "New"
Then I should be at "/thing/new"
When I fill in "title" with <title>
When I fill in "description" with "description"
When I click on "save"
Then I should be at "/things"
Then I should see <title> in the list
When I click on <title>
Then I should see <title>
Then I should see <description>
Examples:
| title | description |
| "My new thing" | $myvar |
回答1:
If it was me I'd write the Scenario at a much higher level. What is the requirement you're describing? If it's that "Things" can have descriptions of up to 500 characters (or whatever), then say that instead of having an arbitrary long string:
When I fill in "description" with a 500 character description
....
Then the new thing should have a description 500 characters long
And the new thing's description should match the description entered
Your When step implementation could then generate 500 characters of Lorem Ipsum data, enter it into the form and store it in the Scenario context for checking later.
It isn't pretty but:
- It describes the requirement better than an random "long string"
- It keeps the Feature files cleaner and more concise
It might be worth applying the same "What requirement am I describing?" question to the rest of the scenario as well. There's a lot going on here that personally I'd split into multiple Scenarios.
回答2:
I don't think we can have variables as you exemplified with $myvar
. If James McCalden's suggestion does not suit you, having the content in an external file may be close enough to your $myvar
suggestion, e.g.:
Scenario Outline: create a thing
When I ...
...
Then I should see <title>
Then I should see <description>
Examples:
| title | description |
| "My new thing" | /mydir/myvar.txt |
Then /mydir/myvar.txt
in the test resources directory would contain:
a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string
And finally in your steps class:
@Then("Then I should see (.+)")
public void thenISouldSee(String param) {
param = process(param);
...
}
private String process(String parameter) throws IOException, URISyntaxException {
return parameter.charAt(0) == '/' ? readFile(parameter) : parameter;
}
private String readFile(String dir) throws IOException, URISyntaxException {
File file = new File(BrandsContentManagementSteps.class.getResource(dir).toURI());
return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
}
Note that FileUtils requires importing apache commons.io.
来源:https://stackoverflow.com/questions/21016364/dealing-with-a-large-string-in-gherkin-with-scenario-outline