在托管 C++ 测试中使用 TestContext.TestName
Using TestContext.TestName in Managed C++ Tests
如何在托管 VS C++ 测试代码中使用 TestContext class 的 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
一位同事为我回答了这个问题。如果您创建一个名为 TestInstance 的 public 成员 属性,框架将自动为您设置测试上下文。这是适合我的确切语法。
#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()
{
}
};
如何在托管 VS C++ 测试代码中使用 TestContext class 的 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
一位同事为我回答了这个问题。如果您创建一个名为 TestInstance 的 public 成员 属性,框架将自动为您设置测试上下文。这是适合我的确切语法。
#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()
{
}
};