C++ Catch 是否有类似 NUnit 测试用例的东西,有多个 parameter/input 选项
Does C++ Catch has something like NUnit's TestCase with multiple parameter/input options
NUnit 具有以下功能,您可以在其中为具有 TestCase 属性的测试指定不同的值。 Catch有类似的东西吗?
[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual( q, n / d );
}
我需要 运行 具有不同数据值的相同单元测试,但每个都是不同的单元测试。我可以 copy/paste TEST_CASE/SECTION 并更改值,但是有没有一种像 NUnit 那样干净的方法来做到这一点。
我发现很难弄清楚要搜索什么。 Catch 使用 TEST_CASE 进行单元测试,这与 NUnit 调用的 TestCase 完全不同。
我找不到与您正在寻找的内容相当的内容,但您根本不需要复制和粘贴所有内容:
#include "catch.hpp"
// A test function which we are going to call in the test cases
void testDivision(int n, int d, int q)
{
// we intend to run this multiple times and count the errors independently
// so we use CHECK rather than REQUIRE
CHECK( q == n / d );
}
TEST_CASE( "Divisions with sections", "[divide]" ) {
// This is more cumbersome but it will work better
// if we need to use REQUIRE in our test function
SECTION("by three") {
testDivision(12, 3, 4);
}
SECTION("by four") {
testDivision(12, 4, 3);
}
SECTION("by two") {
testDivision(12, 2, 7); // wrong!
}
SECTION("by six") {
testDivision(12, 6, 2);
}
}
TEST_CASE( "Division without Sections", "[divide]" ) {
testDivision(12, 3, 4);
testDivision(12, 4, 3);
testDivision(12, 2, 7); // oops...
testDivision(12, 6, 2); // this would not execute because
// of previous failing REQUIRE had we used that
}
TEST_CASE ("Division with loop", "[divide]")
{
struct {
int n;
int d;
int q;
} test_cases[] = {{12,3,4}, {12,4,3}, {12,2,7},
{12,6,2}};
for(auto &test_case : test_cases) {
testDivision(test_case.n, test_case.d, test_case.q);
}
}
NUnit 具有以下功能,您可以在其中为具有 TestCase 属性的测试指定不同的值。 Catch有类似的东西吗?
[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual( q, n / d );
}
我需要 运行 具有不同数据值的相同单元测试,但每个都是不同的单元测试。我可以 copy/paste TEST_CASE/SECTION 并更改值,但是有没有一种像 NUnit 那样干净的方法来做到这一点。
我发现很难弄清楚要搜索什么。 Catch 使用 TEST_CASE 进行单元测试,这与 NUnit 调用的 TestCase 完全不同。
我找不到与您正在寻找的内容相当的内容,但您根本不需要复制和粘贴所有内容:
#include "catch.hpp"
// A test function which we are going to call in the test cases
void testDivision(int n, int d, int q)
{
// we intend to run this multiple times and count the errors independently
// so we use CHECK rather than REQUIRE
CHECK( q == n / d );
}
TEST_CASE( "Divisions with sections", "[divide]" ) {
// This is more cumbersome but it will work better
// if we need to use REQUIRE in our test function
SECTION("by three") {
testDivision(12, 3, 4);
}
SECTION("by four") {
testDivision(12, 4, 3);
}
SECTION("by two") {
testDivision(12, 2, 7); // wrong!
}
SECTION("by six") {
testDivision(12, 6, 2);
}
}
TEST_CASE( "Division without Sections", "[divide]" ) {
testDivision(12, 3, 4);
testDivision(12, 4, 3);
testDivision(12, 2, 7); // oops...
testDivision(12, 6, 2); // this would not execute because
// of previous failing REQUIRE had we used that
}
TEST_CASE ("Division with loop", "[divide]")
{
struct {
int n;
int d;
int q;
} test_cases[] = {{12,3,4}, {12,4,3}, {12,2,7},
{12,6,2}};
for(auto &test_case : test_cases) {
testDivision(test_case.n, test_case.d, test_case.q);
}
}