March 21 09:47~12:28
▲Figure 1: Example of Scenario Outline
foreword
The first two episodes introduce how to write Scenarios in ezSpec (please refer to < Using ezSpec to Implement Behavior-Driven Development and Instantiation Requirements (3): Writing Scenarios and Passing Simple Parameters > and < Using ezSpec to Implement Behavior-Driven Development and Instantiation Requirements (4 ): Use the form >) in a Scenario . When you start to use BDD (Behavior Driven Development) or SBE (Requirements by Example) development system, for multiple Scenarios in the same Feature or Story, it is likely that the execution steps will be the same or similar, only the input data and the data used in When To check the expected result different situations. At this point, consider rewriting these Scenarios into Scenario Outlines to simplify the writing of feature files.
ezSpec supports four different ways to write and execute Scenario Outline, which are introduced one by one below.
***
Method 1: Execute Scenario Outline with For Each
Scenario is translated into plot or scene , and Scenario Outline is plot outline or scenario outline . Simply put, Scenario Outline is to feed a set of data to Scenario and let the Scenario execute several times. In traditional testing terms, such tests are called parameterized test cases or data-driven test cases .
Figure 1 is a simple Scenario Outline, the purpose of which is to calculate the total tax-included price based on the tax-free amount of the product. There are four examples (four test data), covering rounding, zero-yuan invoices and two boundary conditions. . In order to write these four examples, the most brainless way is to write them as four Scenarios. But in this way, except for the different test data, the other execution steps of these four scenarios are the same, that is to say, writing four scenarios will generate a lot of repetitive codes. At this time, using Scenario Outline can solve this problem.
In order to integrate with JUnit 5 as much as possible, ezSpec provides several ways to write Scenario Outline. First see Figure 2:
- Line 74: Execute newScenarioOutline() to generate a new Scenario Outline on Story.
- Line 75: Use WithExamples(examples) to specify the Examples (that is, test data) used by the Scenario Outline.
- Line 76: Call RuntimeScenarios() to get List<RunimeScenario>. Scenario Outline will generate N RunimeScenarios according to the number of examples input.
- Line 77: Use forEach(example -> {} ) to specify the Step of executing RunimeScenario every time.
The subsequent writing method is the same as the general Scenario, the only difference is the way to read the Examples parameter. In a standard scenario, the data in the table can be read through env.row(). In the case of Scenario Outline, RunimeScenario will only see one piece of Examples data each time. At this time, use env.getInput().get(column_name) to read the Examples data, as shown in lines 82-85 in Figure 2 shown.
▲Figure 2: Scenario Outline example of ezSpec, executed with forEach
Since the Scenario Outline in Figure 2 is just a general standard test method, only one execution result will be seen in the JUnit report, as shown in Figure 3.
▲Figure 3: JUnit report of the execution results in Figure 2
Although there is only one piece of data in the JUnit report, the report generated by ezSpec will include the execution results of Scenario Outline each time, please refer to Figure 4.
▲Figure 4: ezSpect report of the execution results in Figure 2
***
Method 2: Execute Scenario Outline with @ParameterizedTest
If developers want to see the results of each execution of Scenario Outline in the JUnit report, they can use @ParameterizedTest of JUnit 5 to execute Scenario Outline, please refer to Figure 5. ParameterizedTest of JUnit 5 directly receives the test data for each execution through the arguments of the test method. ParameterizedTest supports a variety of methods for specifying test data (equivalent to the Examples in Figure 1). Figure 5 shows the way to specify test data through @CsvSource .
The method of using ezSpec to write Scenario Outline through ParameterizedTest is similar to that in Figure 2, except for replacing the original @Test with @ParameterizedTest and using the method of setting test data specified by @ParameterizedTest, there are two main differences:
- No need to use for each by yourself : Because ParameterizedTest of JUnit 5 will automatically execute the test method several times, developers do not need to execute RuntimeScenarios through RuntimeScenarios().forEach as shown in Figure 2.
- Replace WithExamples in Figure 2 with WithParameterizedExamples : Since the test data of ParameterizedTest does not include the header of the table, it is necessary to inform ezSpec of the header of the test data through WithParameterizedExamples. In this way, when describing each Step, if <tax_excluded> is used When waiting for variables, ezSpec knows how to replace these variables with the actual values in the test data.
▲Figure 5: Scenario Outline of executing ezSpec using @ParameterizedTest
One advantage of using ParameterizedTest is that the JUnit report can display the execution results of each Scenario Outline, as shown in Figure 6.
▲Figure 6: JUnit report of the execution results in Figure 5
***
Method 3: Execute Scenario Outline with @EzScenarioOutline
Developers can also use ezSpec’s @EzScenarioOutline to execute Scenario Outline, as shown in Figure 7. In this case, there is no need to use RuntimeScenarios().forEach to execute RuntimeScenarios. The content of ScenarioOutline is basically the same as Scenario except for calling WithExamples. There are three main differences:
- @EzScenarioOutline needs to execute the number of executions by itself. Take the example in Figure 7, because there are four test data, so specify @EzScenarioOutline (4)
- The test method needs to accept a parameter whose type is RepetitionInfo , as shown in line 119 in Figure 7.
- Use Execute(repetitionInfo.getCurrentRepetition()) instead to execute RuntimeScenarios, as shown in line 138 in Figure 7.
▲Figure 7: Scenario Outline of executing ezSpec using @ParameterizedTest
Using @EzScenarioOutline can achieve a similar effect to @ParameterizedTest, and display the execution results of each Scenario Outline in the JUnit report (but the text description cannot be displayed), as shown in Figure 8.
▲Figure 8: JUnit report of the execution results in Figure 7
***
Method 4: Execute Scenario Outline with @DynamicScenario
The last way to execute Scenario Outline is to use @DynamicScenario of ezSpec, please refer to Figure 9. @DynamicScenario is written in much the same way as @EzScenarioOutline, the differences are:
- The test method must return DynamicNode , as shown in line 141 in Figure 9.
- Replace Execute( ) with DynamicExecute( ) , line 160 in Figure 9.
- The entire DynamicExecute execution result needs to be returned , as shown in line 149 in Figure 9.
▲Figure 9: Using @DynamicScenario to execute Scenario Outline of ezSpec
Using @DynamicScenario can display the most detailed information in the JUnit report. In addition to the execution results of each Scenario Outline, it also includes the content and execution results of each Step, as shown in Figure 10.
▲Figure 10: JUnit report of the execution results in Figure 9
***
epilogue
This episode introduces four different methods of writing and executing Scenario Outline. Except for slightly different writing methods and reports generated by JUnit, the execution results and reports generated by ezSpec are basically the same. Developers can choose their favorite and French expression to write Scenario Outline.
The Examples of Scenario Outline in this episode is a simple table, and the next episode will introduce how to specify the test data containing the table in the Scenario Outline.
***
Yuzo’s inner monologue: Too many choices are also a kind of trouble.
This article is transferred from https://teddy-chen-tw.blogspot.com/2023/03/ezspec5scenario-outline.html
This site is only for collection, and the copyright belongs to the original author.