1

我还没有找到一种方法来转义注释,使用的 CSV 资源文件中的字符。CsvFileSource junit5因此,任何包含逗号的字符串都会被切成两半,并且永远不会使用第二部分。

有一些解决方法吗?


编辑:原始问题不完整。问题是我的资源中有逗号和双引号。参数化测试处理引号,但不处理两者。

CSV 行示例:

5,10,5,53,"</identity/partners?limit=5&cursor=5>; rel="prev", </identity/partners?limit=5&cursor=15>; rel="next""

引号被正确转义,但只有在出现逗号之前(这是我可能猜到的算法)。

所以生成的断言看起来像这样:

"Link" was not "\"</identity/partners?limit=5&cursor=5>; rel=\"prev\"", was "</identity/partners?limit=5&cursor=5>; rel="prev", </identity/partners?limit=5&cursor=15>; rel="next""
4

2 回答 2

4

将您的值用双引号括起来"first, value", second, third and last one

更多细节在这里:http: //junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-CsvFileSource

在您的情况下,您可以在注释中指定一个完全不同的分隔符: https ://junit.org/junit5/docs/current/api/org.junit.jupiter.params/org/junit/jupiter/params/provider/ CsvFileSource.html#delimiter()

但是,它不再是“逗号”分隔值......

于 2017-11-30T12:05:01.410 回答
0

如果有人登陆此页面寻找答案,这是我在测试中使用的示例,以实现能够在测试中使用这些字符串值的目标(注意:它不会转义逗号,但可以实现运行测试的目标在输入中包含逗号。您可以使用任何您想要的分隔符)。

@ParameterizedTest(name = "Product details for product number - {0}")
@CsvSource(value = {"0; Sauce Labs Backpack;carry.allTheThings() with the sleek, streamlined Sly Pack that melds uncompromising style with unequaled laptop and tablet protection."
        ,"1; Sauce Labs Bike Light;A red light isn't the desired state in testing but it sure helps when riding your bike at night. Water-resistant with 3 lighting modes, 1 AAA battery included."
}, delimiter = ';')
void assertThatProductDescriptionIsCorrectForAStandardUser(Integer productNumber, String productSummary, String productDescription) {
    String url = String.format("%s/%s", swagItemDetails, productNumber);
    deepLink.deepLinkToScreen(url , packageName);

    assertAll("Product Details"
            , () -> assertEquals(productSummary, productScreen.getProductSummary())
            , () -> assertEquals(productDescription, productScreen.getProductDescriptionByText(productDescription))
    );
}
于 2021-10-11T18:50:41.360 回答