Microsoft::VisualStudio::CppUnitTestFramework中的参数化测试方法 我用C++来编写一些C++项目的测试用例:微软::VisualStudio::CppUnitTestFramework < /强>。这里我有一个案例,我必须用不同的参数运行同一个测试用例
在nunitframeworkforcpp中,我可以通过以下代码实现这一点Microsoft::VisualStudio::CppUnitTestFramework中的参数化测试方法 我用C++来编写一些C++项目的测试用例:微软::VisualStudio::CppUnitTestFramework < /强>。这里我有一个案例,我必须用不同的参数运行同一个测试用例,c++,unit-testing,nunit,cppunit,microsoft-cpp-unit-test,C++,Unit Testing,Nunit,Cppunit,Microsoft Cpp Unit Test,在nunitframeworkforcpp中,我可以通过以下代码实现这一点 [Test, SequentialAttribute] void MyTest([Values("A", "B")] std::string s) { } 通过传递这些参数,此测试将运行2次 MyTest("A") MyTest("B") 在Microsoft::VisualStudio::CPPFUnitTestFramework单元测试中是否有类似的方法来实现这一点 非常感谢您的帮助 我有一个类似的问题:我有一
[Test, SequentialAttribute]
void MyTest([Values("A", "B")] std::string s)
{
}
通过传递这些参数,此测试将运行2次
MyTest("A")
MyTest("B")
在Microsoft::VisualStudio::CPPFUnitTestFramework单元测试中是否有类似的方法来实现这一点
非常感谢您的帮助 我有一个类似的问题:我有一个接口和它的几个实现。当然,我只想针对接口编写测试。另外,我不想复制每个实现的测试。因此,我寻找了一种将参数传递给测试的方法。嗯,我的解决方案不是很漂亮,但它很简单,是我迄今为止唯一想到的解决方案 以下是我对问题的解决方案(在您的情况下,测试下的类\u将是您希望传递到测试中的参数): 设置.cpp
#include "stdafx.h"
class VehicleInterface
{
public:
VehicleInterface();
virtual ~VehicleInterface();
virtual bool SetSpeed(int x) = 0;
};
class Car : public VehicleInterface {
public:
virtual bool SetSpeed(int x) {
return(true);
}
};
class Bike : public VehicleInterface {
public:
virtual bool SetSpeed(int x) {
return(true);
}
};
#define CLASS_UNDER_TEST Car
#include "unittest.cpp"
#undef CLASS_UNDER_TEST
#define CLASS_UNDER_TEST Bike
#include "unittest.cpp"
#undef CLASS_UNDER_TEST
unittest.cpp
#include "stdafx.h"
#include "CppUnitTest.h"
#define CONCAT2(a, b) a ## b
#define CONCAT(a, b) CONCAT2(a, b)
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
TEST_CLASS(CONCAT(CLASS_UNDER_TEST, Test))
{
public:
CLASS_UNDER_TEST vehicle;
TEST_METHOD(CONCAT(CLASS_UNDER_TEST, _SpeedTest))
{
Assert::IsTrue(vehicle.SetSpeed(42));
}
};
您需要将“unittest.cpp”从构建中排除。CppUnitTestFramework不提供参数化测试,但没有什么可以阻止您编写参数化函数并从测试中调用它
void MyTest(char *param)
{
// Actual test code here
}
TEST_METHOD(MyTest_ParamA)
{
MyTest("A");
}
TEST_METHOD(MyTest_ParamB)
{
MyTest("B");
}
快速简单的解决方案: 在test_METHOD_INITIALIZE中使用您的测试用例创建一个向量,然后在每个测试用例中迭代该向量
#include "stdafx.h"
#include "CppUnitTest.h"
#include <vector>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace SomeTests
{
TEST_CLASS(Some_Tests)
{
public:
std::vector<int> myTestCases;
TEST_METHOD_INITIALIZE(Initialize_Test_Cases)
{
myTestCases.push_back(1);
myTestCases.push_back(2);
myTestCases.push_back(3);
}
TEST_METHOD(Test_GreaterThanZero)
{
for (auto const& testCase : myTestCases)
{
Assert::IsTrue(testCase > 0);
}
}
};
}
#包括“stdafx.h”
#包括“CppUnitTest.h”
#包括
使用名称空间Microsoft::VisualStudio::CpUnitTestFramework;
命名空间测试
{
测试类(一些测试)
{
公众:
std::病媒myTestCases;
测试方法初始化(初始化测试用例)
{
myTestCases.向后推(1);
myTestCases。推回(2);
myTestCases.向后推(3);
}
测试方法(测试大于零)
{
用于(自动常量和测试用例:myTestCases)
{
Assert::IsTrue(testCase>0);
}
}
};
}
这没问题-但是我怎么知道哪个测试失败了?如果我的测试是Assert::IsTrue(testCase%2==1)
那么我会让2个案例失败,但是我得到的所有信息是“test\u IsOdd”(从大于零的地方重命名)失败了,而不是哪个“case”-对吗?