在 Kotlin 中编写单元测试,共享变量?
Writing Unit tests in Kotlin, sharing variables?
我正在尝试在 Kotlin 中创建一些功能测试,以使用 Rest Assured 库向购物车 Java 服务发出请求。
因为我希望测试按程序进行,所以我希望我可以存储第一个 API 请求的结果并将其传递给下一个单元测试。
即
createCartTest() --> cartId --> getCartForWebsiteTest(cartId)
class CartTest : RestAssuredSupport {
val port = 8080
val url = "http://localhost:"
val cartId = null
/**
* Create a cart object
*/
@Test fun createCartTest() {
given().
now().
body("websiteId=1").
contentType(ContentType.URLENC).
post(url + port + "/orders/cart/create.do").
then().
statusCode(200).
body("summary.numItems", equalTo(0)).
body("summary.visibleNumItems", equalTo(0)).
body("summary.cartId", notNullValue()).
body("summary.version", notNullValue())
}
/**
* Fetch a cart object created by websiteId and cartId
*/
@Test fun getCartForWebsite() {
given().
now().
body("websiteId=1&cartId=" + cartId).
contentType(ContentType.URLENC).
post(url + port + "/orders/cart/getCartForWebsite.do").
then().
statusCode(200).
body("summary.numItems", equalTo(0)).
body("summary.visibleNumItems", equalTo(0)).
body("summary.cartId", equalTo(cartId)).
body("summary.version", notNullValue())
}
}
从未真正使用过 Kotlin,因此寻求有关测试所有 API 端点是否正常工作的最佳方法的建议。
或者在同一个函数中发出另一个请求并将结果传递给下一步会更好吗?
跨测试共享变量的最佳方式是什么?
谢谢
单元测试应该是独立的。
默认情况下,您无法控制单元测试的执行顺序
已阅读此处:How to run test methods in specific order in JUnit4?
"Or would it be better to make another request inside the same function and pass the result to the next step?"
没错,我们通常称之为旅程测试
JUnit 不提供任何关于测试顺序的保证,测试不应相互依赖,也不应改变共享状态。
确实,最简单的解决方案是在同一测试中执行这两个请求。
此外,您可以使用 JUnit test fixtures 设置一组测试所需的对象。如果您在多个测试中需要 cartId
,那是首选。
class CartTest : RestAssuredSupport {
// ...
var cartId: Int by Delegates.notNull()
@Before fun setUp() {
// Set up the cartId. This will run before each test. Use @BeforeClass to run once
}
@Test fun getCartForWebsite() { /* ... */ }
}
参见:Delegates.notNull()
, another question about @Before
and @BeforeClass
。
如果您在许多测试中有共同的设置过程,我倾向于同意上面的 @Before
建议,但否则您应该使每个测试都自包含。 Kotlin 可以让这变得非常好 - 请参阅 https://gjesse.github.io/post/unit-testing-with-kotlin---mini-dsls/ 了解为单个测试包装常见操作类型的描述。
我正在尝试在 Kotlin 中创建一些功能测试,以使用 Rest Assured 库向购物车 Java 服务发出请求。
因为我希望测试按程序进行,所以我希望我可以存储第一个 API 请求的结果并将其传递给下一个单元测试。
即
createCartTest() --> cartId --> getCartForWebsiteTest(cartId)
class CartTest : RestAssuredSupport {
val port = 8080
val url = "http://localhost:"
val cartId = null
/**
* Create a cart object
*/
@Test fun createCartTest() {
given().
now().
body("websiteId=1").
contentType(ContentType.URLENC).
post(url + port + "/orders/cart/create.do").
then().
statusCode(200).
body("summary.numItems", equalTo(0)).
body("summary.visibleNumItems", equalTo(0)).
body("summary.cartId", notNullValue()).
body("summary.version", notNullValue())
}
/**
* Fetch a cart object created by websiteId and cartId
*/
@Test fun getCartForWebsite() {
given().
now().
body("websiteId=1&cartId=" + cartId).
contentType(ContentType.URLENC).
post(url + port + "/orders/cart/getCartForWebsite.do").
then().
statusCode(200).
body("summary.numItems", equalTo(0)).
body("summary.visibleNumItems", equalTo(0)).
body("summary.cartId", equalTo(cartId)).
body("summary.version", notNullValue())
}
}
从未真正使用过 Kotlin,因此寻求有关测试所有 API 端点是否正常工作的最佳方法的建议。
或者在同一个函数中发出另一个请求并将结果传递给下一步会更好吗?
跨测试共享变量的最佳方式是什么?
谢谢
单元测试应该是独立的。 默认情况下,您无法控制单元测试的执行顺序 已阅读此处:How to run test methods in specific order in JUnit4?
"Or would it be better to make another request inside the same function and pass the result to the next step?"
没错,我们通常称之为旅程测试
JUnit 不提供任何关于测试顺序的保证,测试不应相互依赖,也不应改变共享状态。
确实,最简单的解决方案是在同一测试中执行这两个请求。
此外,您可以使用 JUnit test fixtures 设置一组测试所需的对象。如果您在多个测试中需要 cartId
,那是首选。
class CartTest : RestAssuredSupport {
// ...
var cartId: Int by Delegates.notNull()
@Before fun setUp() {
// Set up the cartId. This will run before each test. Use @BeforeClass to run once
}
@Test fun getCartForWebsite() { /* ... */ }
}
参见:Delegates.notNull()
, another question about @Before
and @BeforeClass
。
如果您在许多测试中有共同的设置过程,我倾向于同意上面的 @Before
建议,但否则您应该使每个测试都自包含。 Kotlin 可以让这变得非常好 - 请参阅 https://gjesse.github.io/post/unit-testing-with-kotlin---mini-dsls/ 了解为单个测试包装常见操作类型的描述。