2016-03-18 72 views
1

如何在託管VS C++測試代碼中使用TestContext類的TestName成員自動將測試方法的名稱輸出到調試控制檯?在託管C++測試中使用TestContext.TestName

我已經能夠找到的每個例子都在C#中,我無法正確地將它翻譯成C++。在這裏,我試圖通過在靜態ClassInitialize方法中捕獲TestContext對象來做到這一點,但這不起作用。

#include <windows.h> 
#include <msclr/marshal_cppstd.h> 

using namespace Microsoft::VisualStudio::TestTools::UnitTesting; 

[TestClass] 
public ref class SampleTestClass 
{ 

public: 

    [TestMethod] 
    void testMethod1() 
    { 

    } 

    [TestMethod] 
    void testMethod2() 
    { 

    } 

    [TestMethod] 
    void testMethod3() 
    { 

    } 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
// Tests Setup and Teardown                     // 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    static TestContext^ myTestContext; 

    [TestInitialize] 
    void testCaseInitialize() 
    { 
     msclr::interop::marshal_context context; 
     std::wstring testName = context.marshal_as<std::wstring>(myTestContext->TestName); 
     std::wstring dbgSend = L"initializing " + testName; 
     ::OutputDebugString(dbgSend.c_str()); 
    } 

    [TestCleanup] 
    void testCaseCleanup() 
    { 
     msclr::interop::marshal_context context; 
     std::wstring testName = context.marshal_as<std::wstring>(myTestContext->TestName); 
     std::wstring dbgSend = L"tearing down " + testName; 
     ::OutputDebugString(dbgSend.c_str()); 
    } 

    [ClassInitialize] 
    static void testClassInitialize(TestContext^ context) 
    { 
     myTestContext = context; 
    } 

    [ClassCleanup] 
    static void testClassCleanup() 
    { 

    } 



}; 

輸出

[9404] initializing testMethod1 
[9404] tearing down testMethod1 
[9404] initializing testMethod1 
[9404] tearing down testMethod1 
[9404] initializing testMethod1 
[9404] tearing down testMethod1 

所需的輸出

[9404] initializing testMethod1 
[9404] tearing down testMethod1 
[9404] initializing testMethod2 
[9404] tearing down testMethod2 
[9404] initializing testMethod3 
[9404] tearing down testMethod3 

回答

0

一位同事回答了這個問題對我來說。如果您創建名爲TestInstance的公共成員屬性,框架將自動爲您設置測試上下文。這裏是適合我的確切語法。

#include <windows.h> 
#include <msclr/marshal_cppstd.h> 

using namespace Microsoft::VisualStudio::TestTools::UnitTesting; 

[TestClass] 
public ref class SampleTestClass 
{ 

public: 

    [TestMethod] 
    void testMethod1() 
    { 

    } 

    [TestMethod] 
    void testMethod2() 
    { 

    } 

    [TestMethod] 
    void testMethod3() 
    { 

    } 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
// Tests Setup and Teardown                     // 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    private: 
    TestContext^ myTestContextInstance; 

    public: 
    property TestContext^ TestContext 
    { 

     virtual Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ get() 
     { 
      return myTestContextInstance; 
     } 

     virtual void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ value) 
     { 
      myTestContextInstance = value; 
     } 
    } 


    [TestInitialize] 
    void testCaseInitialize() 
    { 
     msclr::interop::marshal_context context; 
     std::wstring testName = context.marshal_as<std::wstring>(myTestContextInstance->TestName); 
     std::wstring dbgSend = L"initializing " + testName; 
     ::OutputDebugString(dbgSend.c_str()); 
    } 

    [TestCleanup] 
    void testCaseCleanup() 
    { 
     msclr::interop::marshal_context context; 
     std::wstring testName = context.marshal_as<std::wstring>(myTestContextInstance->TestName); 
     std::wstring dbgSend = L"tearing down " + testName; 
     ::OutputDebugString(dbgSend.c_str()); 
    } 

    [ClassInitialize] 
    static void testClassInitialize(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ context) 
    { 

    } 

    [ClassCleanup] 
    static void testClassCleanup() 
    { 

    } 

};