问题
I am on a team, presenting the advantages of Karate to move forward as the framework of choice for our API testing. However, I have come across a couple questions, in regards to data-driven testing.
I have gone through the documentation, csv files and cannot find a solution for this question:
- Is Karate capable of executing tests on specific data sets (For instance, based on priority p0, p1) given in a csv file?
Example "test.csv":
|priority|data1|
| p0 | 1 |
| p0 | 2 |
| p1 | 4 |
| p1 | 6 |
I want to run my test cases with specific data sets in a csv file (p0, or p1, or both). Is Karate capable of this?
回答1:
There are multiple ways I would do this, here is one:
Background:
* def data = read('test.csv')
* def selected = 'p1'
* def fun = function(x){ return x.priority == selected }
* def filtered = karate.filter(data, fun)
Scenario Outline:
* print __row
Examples:
| filtered |
You don't need to force yourself into a Scenario Outline
, you can loop over data and ignore the rows where you don't want to do any processing.
Refer to this answer for more ideas: https://stackoverflow.com/a/61685169/143475
Note that you can "fall back" to Java for advanced logic if needed: https://github.com/intuit/karate#calling-java
来源:https://stackoverflow.com/questions/63387913/karate-ability-to-execute-tests-on-a-specific-set-of-data-in-a-csv-file