我正在使用Behat进行BDD并使用Scenario Outlines,因此我可以轻松地对其他数据进行相同的测试。但是我遇到了大文本的问题。见下面的例子:
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" |
如您所能想象的那样,如果有更多大型文本或更多类型的值,这可能很烦人。这有解决方案吗?例如使用变量?这可能是这样的:
$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 |
答案 0 :(得分:2)
如果是我,我会在更高的层次上编写场景。您要描述的要求是什么?如果是“Things”可以包含多达500个字符(或其他)的描述,那么请说明不要使用任意长字符串:
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
您的步骤实现然后可以生成500个字符的Lorem Ipsum数据,将其输入到表单中并将其存储在Scenario上下文中以供稍后检查。
它不漂亮但是:
可能值得应用“我描述的要求是什么?”对问题的其余部分也提出质疑。这里有很多事情我个人分成多个场景。
答案 1 :(得分:0)
我不认为您可以使用$myvar
来表示变量。如果James McCalden的建议不适合您,则将外部文件中的内容与您的$myvar
建议相距很近,例如:
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 |
然后 test resources 目录中的/mydir/myvar.txt
将包含:
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
最后在您的步骤课中:
@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);
}
请注意,FileUtils需要导入apache commons.io。