I'm new to Spock, and currently switching to it, but I inherited plenty of test configuration files which need to be re-used. Each config file is a JSON, having the same name as Spec class. For each test method there is a list of maps with parameters, e.g.:
LoginSpec.json:
{
"My first test": [
{
"user": "user_one",
"role": "ADMIN"
},
{
"user": "user_two",
"role": "REPORTER",
"other_param": "other"
}
],
"Some Other Test Method": [
{
"url": "/lab1",
"button_name": "Show news popup"
}
]
}
TestNG allowed me to pass test method name in data provider method, so I could return the list of maps depending on test class name and test method name. I had only one data provider method in my base class:
public Object[][] getData(String method) {
DataReader reader = new JsonReader()
return reader.parse(packageFullName, getClass().simpleName, method)
}
As a result of this method I get an array of Maps to use in each of test iteration. And then I just specify this method as a DataProvider:
@Test(dataProvider = "getData", priority = 1)
void EULA_1(Map data) { <====
Pages.login.openLoginPage()
Pages.login.logIn(data.user) <====
...
}
This works perfectly: declared ones in the base class, it automatically receives the test and provides test data.
The question is: is there a way to apply similar approach in Spock tests?
I'd like to have some getData() method in my base class, where I'm able to read tests parameters depending on the test method name and then pass them into where block.
I tried to use my json reader as shown below:
def "My first test"() {
setup:
println(data)
when:
...
then:
...
where:
data = dataReader.parse("JobE2E", "LoginSpec.json", "My first test")
}
This example gives me required list of maps, but has two problems:
- data here - is the full list of maps, not one map for each iteration;
- I'm forced to explicitly type the name of test method, class etc.
Summing up: What is the best way to implement a data provider which will receive test method name and return a list of maps?
Aucun commentaire:
Enregistrer un commentaire