2017-02-15 100 views
1

我正在爲我的C++項目編寫一些測試用例,使用Microsoft :: VisualStudio :: CppUnitTestFramework。在這裏,我有一個情況,我必須運行不同參數的相同測試用例。Microsoft :: VisualStudio中的參數化測試方法:: CppUnitTestFramework

在用於CPP的Nunit框架中,我可以通過以下代碼實現此目的。

[Test, SequentialAttribute] 
void MyTest([Values("A", "B")] std::string s) 
{ 

} 

通過傳遞這些參數,該測試將運行2次。

MyTest("A") 
MyTest("B") 

有沒有類似的方式在微軟的VisualStudio :: :: CppUnitTestFramework單元測試來實現這一目標。

任何幫助,高度讚賞。

回答

0

我有一個類似的問題:我有一個接口和它的幾個實現。當然,我只想對接口編寫測試。另外,我不想複製每個實現的測試。因此,我搜索了一種將參數傳遞給我的測試的方法。那麼,我的解決方案不是很漂亮,但它很直接,也是我到現在爲止唯一的解決方案。

這是我對我的問題的解決方案(在你的情況CLASS_UNDER_TEST將要傳遞到測試參數):

setup.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)); 
    } 
}; 

您將需要從build中排除「unittest.cpp」。