admin管理员组文章数量:1240593
I have 2 endpoints that do the same: receive the same parameters, and respond with identical responses, the only difference is a baseUrl. I have about 50+ parameterized tests under ApiTests class
public class ApiTests {
@ParameterizedTest
@EmptySource
@ValueSource(strings = {"", " ", "abc", "ABC", "true", "adsfkjsdgfhgsjdgfjgshdjgfgsd"})
@Description("Get valid customer info")
public void testGetData(String customer) {
Response getCustomerData = getCustomerDataResponse(customer);
//Do validation
}
@ParameterizedTest
@ValueSource(ints = {Integer.MIN_VALUE, 0, 100, Integer.MAX_VALUE})
@Description("Get invalid customer info")
public void testGetData2(int customer) {
Response getCustomerData = getCustomerDataResponse(customer);
//Do validation;
}
//more tests
public Response getCustomerDataResponse(Object customer) {
Response getCustomerData = given().
header("Authorization", "Bearer " + token).
pathParam("customer", customer).
contentType(ContentType.JSON).
when().
get("abc/v1/customers/{customer}");
return getCustomerData;
}
}
I want to run the entire class of tests once with "abc/v1/customers/{customer}" and after that with "abcdef/v1/customers/{customer}" It should be a part of a nightly run for a long period of time. Any idea how to do it without copy pasting the entire class just because it should run with a different baseUrl?
Because I already have parametrized tests I can't put the base url as a parameter for my test methods.
I have 2 endpoints that do the same: receive the same parameters, and respond with identical responses, the only difference is a baseUrl. I have about 50+ parameterized tests under ApiTests class
public class ApiTests {
@ParameterizedTest
@EmptySource
@ValueSource(strings = {"", " ", "abc", "ABC", "true", "adsfkjsdgfhgsjdgfjgshdjgfgsd"})
@Description("Get valid customer info")
public void testGetData(String customer) {
Response getCustomerData = getCustomerDataResponse(customer);
//Do validation
}
@ParameterizedTest
@ValueSource(ints = {Integer.MIN_VALUE, 0, 100, Integer.MAX_VALUE})
@Description("Get invalid customer info")
public void testGetData2(int customer) {
Response getCustomerData = getCustomerDataResponse(customer);
//Do validation;
}
//more tests
public Response getCustomerDataResponse(Object customer) {
Response getCustomerData = given().
header("Authorization", "Bearer " + token).
pathParam("customer", customer).
contentType(ContentType.JSON).
when().
get("abc/v1/customers/{customer}");
return getCustomerData;
}
}
I want to run the entire class of tests once with "abc/v1/customers/{customer}" and after that with "abcdef/v1/customers/{customer}" It should be a part of a nightly run for a long period of time. Any idea how to do it without copy pasting the entire class just because it should run with a different baseUrl?
Because I already have parametrized tests I can't put the base url as a parameter for my test methods.
Share Improve this question edited yesterday jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked yesterday elagelag 111 bronze badge 6 | Show 1 more comment1 Answer
Reset to default 0One solution I've used myself a few times is to create an abstract class, put all the test methods in there, and sub class it. You can even nest them in your existing class:
class ApiTests {
@Nested
class AbcApiTests extends ApiEndpointTests {
AbcApiTests() {
super("abc");
}
}
@Nested
class AbcdefApiTests extends ApiEndpointTests {
AbcdefApiTests() {
super("abcdef");
}
}
abstract static class ApiEndpointTests {
private final String pathPrefix;
ApiEndpointTests(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
// existing test methods with annotations and all
Response getCustomerDataResponse(Object customer) {
Response getCustomerData = given().
header("Authorization", "Bearer " + token).
pathParam("customer", customer).
contentType(ContentType.JSON).
when().
get(pathPrefix + "/v1/customers/{customer}");
return getCustomerData;
}
}
}
本文标签: javaHow to run a test class twice with different class parametersStack Overflow
版权声明:本文标题:java - How to run a test class twice with different class parameters? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740049105a2222057.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
@MethodSource
instead and provide more than one parameter to the test, so you can pass both the URL and the value togetCustomerDataResponse()
. – JustAnotherDeveloper Commented yesterdayApiTests
class, with different implementations ofgetCustomerDataResponse
? You could then run JUnit on each subclass. It's an ugly solution, but I think it will do what you want. – Dawood ibn Kareem Commented yesterday