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