[英]JUnit 5, how to get parameterized test parameter from BeforeEach callback?
我在不同的類中有以下一組方法:
@ParameterizedTest
@MethodSource("com.myapp.AppleProvider#getApplesDependingOnConditions")
public void testSomething(Apple apple) {
SomeContainer.getInstance().setApple(apple)
// ...
}
問題是我無法避免復制/粘貼以下內容
name
參數SomeContainer.getInstance().setApple(apple)
我嘗試使用擴展點 - BeforeTestExecutionCallback
和BeforeEachCallback
,但它們似乎無法獲取調用它們的參數。
根據https://github.com/junit-team/junit5/issues/1139和https://github.com/junit-team/junit5/issues/944尚無法訪問從擴展點傳遞給 test 的參數和參數化測試不適用於BeforeEach
回調。
所以我基本上在尋找任何解決方法,以便我的測試看起來像:
@MyAwesomeTest
public void testSomething() {
// ...
}
其中@MyAwesomeTest
封裝了上面的兩個注釋。
我已經發現了什么:
displayname
、 method
或tags
。 如果我將參數傳遞給每個測試方法(盡管它非常不受歡迎)看起來我可以依賴displayname
因為它會反映傳遞給特定參數的方法調用的參數。 我試圖找出是否還有其他方法而無需在每個方法中添加參數。我認為你可以作弊以達到大部分目的:
public static Stream<String> apples() {
return com.myapp.AppleProvider
.getApplesDependingOnConditions()
.stream()
.peek(apple -> SomeContainer.getInstance().setApple(apple))
.map(apple -> { /* convert to name string */ })
}
@ParameterizedTest
@MethodSource("apples")
public void testSomething(String name) {
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.