ExcelBDD Guideline By Java Example
Posted zhangmike
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExcelBDD Guideline By Java Example相关的知识,希望对你有一定的参考价值。
01- Excel BDD Tool Specification By ExcelBDD Method
This tool is to get BDD test data from excel file, its requirent specification is below
The Essential of this approach is obtaining multiple sets of test data, so when combined with Excel's Sheet, the key parameters are:
- ExcelFileName, required, which excel file is used.
- SheetName,optional, which Sheet the requirement writer writes in,if not specified, 1st sheet is chosen. An Excel file supports multiple Sheets, so an Excel is sufficient to support a wide range, such as Epic, Release, or a module.
- HeaderMatcher, filter the header row by this matcher, if matched, this set will be collected in.
- HeaderUnmatcher, filter the header row by this matcher, if matched, this set will be excluded.
Once the header row and parameter name column are determined by 'Parameter Name' grid automaticlly, the data area is determined, such as the green area in the table above. The gray area of the table above is the story step description, which is the general requirements step.
ExcelBDD Java editin is released, below is maven importing code, current version is 0.3.2.
<dependency>
<groupId>com.excelbdd</groupId>
<artifactId>excelbdd-java</artifactId>
<version>0.3.2</version>
</dependency>
There are some typical cases, as described below
02 - Simple Sheet Format
This format is characterized by the use of column A only to illustrate the requirements steps, which are suitable for simple situations.
A java junit5 testcase example is below, suppose the above sheet is the first sheet of excel.xlsx file.
package com.excelbdd.test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import com.excelbdd.Behavior;
import com.excelbdd.TestWizard;
class ExcelBDDSimplestTest {
static Stream<Map<String, String>> provideExampleList() throws IOException {
String filePath = TestWizard.getExcelBDDStartPath("excelbdd-test")
+ "excelbdd-test\\\\src\\\\test\\\\resources\\\\excel.xlsx";
return Behavior.getExampleStream(filePath);
}
@ParameterizedTest(name = "Test{index}:{0}")
@MethodSource("provideExampleList")
void testgetExampleFrom1stSheet(Map<String, String> parameterMap) {
assertNotNull(parameterMap.get("Header"));
for (Map.Entry<String, String> param : parameterMap.entrySet()) {
System.out.println(String.format("%s --- %s", param.getKey(), param.getValue()));
}
//use parameterMap.get("ParamName1") to get any parameter in Excel
//test logic is added here
}
}
03 - Sheet Format With Role Column
Role Column is good for step description. The header and parameter name areas and data areas remain the same as the previous table, making test proofing easy.
Java test code example is almost same as above
//ignore above
class ExcelBDDSimplestTest2 {
static Stream<Map<String, String>> provideExampleList() throws IOException {
String filePath = TestWizard.getExcelBDDStartPath("excelbdd-test")
+ "excelbdd-test\\\\src\\\\test\\\\resources\\\\excel.xlsx";
return Behavior.getExampleStream(filePath,"sheet2");
}
@ParameterizedTest(name = "Test{index}:{0}")
@MethodSource("provideExampleList")
void testgetExampleFrom1stSheet(Map<String, String> parameterMap) {
assertNotNull(parameterMap.get("Header"));
for (Map.Entry<String, String> param : parameterMap.entrySet()) {
System.out.println(String.format("%s --- %s", param.getKey(), param.getValue()));
}
//use parameterMap.get("ParamName1") to get any parameter in Excel
//test logic is added here
}
}
04 - GUI Prototype added in Sheet
interface prototype is put in to show where the parameters are entered clearly.
static Stream<Map<String, String>> provideExampleList() throws IOException {
String filePath = TestWizard.getExcelBDDStartPath("excelbdd-test")
+ "excelbdd-test\\\\src\\\\test\\\\resources\\\\excel.xlsx";
return Behavior.getExampleStream(filePath,"sheet3");
}
05 - Given-When-Then Acceptance Criteria added in Sheet
Focus on the above format, you can see that Excel has enough expressive, directly record text, can carry pictures, although not good at picture production, but can be in another tool after the production of pictures, loaded into the appropriate location, and then provide graphic and lush instructions.
Zoom in on the left side of the image above, as follows.
As you can see, this is a typical Given-When-Then style acceptance criteria, table format writing does not require Gherkin syntax, and tables for multi-group data expansion, with natural advantages, compared to the following Gerkin's table writing:
When the number is small, it looks easy, but when the number is high, it's half a day to go wrong, and it's painful.
Therefore, this is Excel's most significant advantage in supporting BDD.
06 Input vs Expected Format
Java testcase example is below, which uses headerMatcher to filter the data
static Stream<Map<String, String>> provideExampleList() throws IOException {
String filePath = TestWizard.getExcelBDDStartPath("excelbdd-test")
+ "excelbdd-test\\\\src\\\\test\\\\resources\\\\excel.xlsx";
return Behavior.getExampleStream(filePath,"Expected1","Scenario1");
}
@ParameterizedTest(name = "Test{index}:{0}")
@MethodSource("provideExampleList")
void testgetExampleWithExpected(Map<String, String> parameterMap) {
assertNotNull(parameterMap.get("Header"));
System.out.println(String.format("=======Header: %s=======", parameterMap.get("Header")));
for (Map.Entry<String, String> param : parameterMap.entrySet()) {
System.out.println(String.format("%s --- %s", param.getKey(), param.getValue()));
}
}
the output is below
=======Header: Scenario1=======
ParamName4 --- 2021/4/30
ParamName1Expected --- V1.1
Header --- Scenario1
ParamName4Expected --- 2021/4/30
ParamName1 --- V1.1
ParamName2Expected --- V2.1
ParamName2 --- V2.1
ParamName3 ---
ParamName3Expected ---
new Parameters named by "ParameterName + Expected" are added in parameterMap.
07 Input vs Expect + Test Result Format
Java testcase example is below, which uses headerMatcher to filter the data
static Stream<Map<String, String>> provideExampleList() throws IOException {
String filePath = TestWizard.getExcelBDDStartPath("excelbdd-test")
+ "excelbdd-test\\\\src\\\\test\\\\resources\\\\excel.xlsx";
return Behavior.getExampleStream(filePath,"TestResult1","","Scenario2");
}
@ParameterizedTest(name = "Test{index}:{0}")
@MethodSource("provideExampleList")
void testgetExampleWithTestResult(Map<String, String> parameterMap) {
assertNotNull(parameterMap.get("Header"));
System.out.println(String.format("=======Header: %s=======", parameterMap.get("Header")));
for (Map.Entry<String, String> param : parameterMap.entrySet()) {
System.out.println(String.format("%s --- %s", param.getKey(), param.getValue()));
}
}
"Scenario2" is excluded.
Output:
=======Header: Scenario1=======
ParamName3TestResult --- pass
ParamName4 --- 2021/4/30
ParamName4Expected --- 2021/4/30
ParamName4TestResult --- pass
ParamName1 --- V1.1
ParamName2 --- V2.1
ParamName3 ---
ParamName1TestResult --- pass
ParamName2TestResult --- pass
ParamName1Expected --- V1.1
Header --- Scenario1
ParamName2Expected --- V2.1
ParamName3Expected ---
new parameter "ParamName2TestResult" is put in parameterMap.
以上是关于ExcelBDD Guideline By Java Example的主要内容,如果未能解决你的问题,请参考以下文章
ExcelBDD Guideline By Java Example