【问题标题】:Using TestContext.TestName in Managed C++ Tests在托管 C++ 测试中使用 TestContext.TestName
【发布时间】:2016-03-18 17:29:26
【问题描述】:

如何在托管VS 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

【问题讨论】:

    标签: c++ windows visual-studio managed-c++


    【解决方案1】:

    一位同事为我回答了这个问题。如果您创建一个名为 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()
        {
    
        }
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多