使用 Microsoft.VisualStudio.TestTools.CppUnitTestFramework

此主题列出了 Microsoft::VisualStudio::CppUnitTestFramework 命名空间中的公共成员。

头文件在**\VC\UnitTest\include** 文件夹的 VisualStudio2012[x86]InstallFolder中。

LIB 在文件**\VC\UnitTest\lib** 文件夹的 VisualStudio2012[x86]InstallFolder中。

主题内容

CppUnitTest.h

  • 创建测试类和方法

  • 初始化和清理

    • test method — 测试方法

    • Test Class — 测试类

    • 测试模块

  • 创建测试特性

    • TestMethod Attribute — TestMethod 特性

    • TestClass Attribute — TestClass 特性

    • 模块测试特性

    • 预定义特性

    CppUnitTestAssert.h

    • 泛断言

      • 相等

      • 不相等

      • 相同

      • 不相同

      • 为 null。

      • 不为空

      • 为 true.

      • 为 false

      • Fail

    • Windows 运行时断言

      • 相等

      • 相同

      • 不相等

      • 不相同

      • 为 null。

      • 不为空

    • Assert 异常

      • 预期异常

      CppUnitTestLogger.h

      • /logger:

      • 写消息

CppUnitTest.h

创建测试类和方法

TEST_CLASS(className)

对包含测试方法的各个类。标识 className 为测试类。必须声明TEST_CLASSnamescape 大小。

TEST_METHOD(methodName) 
{
    // test method body
}

定义 methodName 为测试方法。必须在声明方法的类范围内,TEST_METHOD。

初始化和清理

test method — 测试方法

TEST_METHOD_INITIALIZE(methodName) 
{
    // method initialization code
}

定义 methodName 作为运行的方法,在每个测试方法之前运行。TEST_METHOD_INITIALIZE 类在测试在测试类只定义一次,因此必须定义。

TEST_METHOD_CLEANUP(methodName) 
{
    // test method cleanup  code
}

定义 methodName 作为运行的方法,在每个测试方法之后运行。在测试类范围内,TEST_METHOD_CLEANUP 在测试类只定义一次,因此必须定义。

Test Class — 测试类

TEST_CLASS_INITIALIZE(methodName) 
{
    // test class initialization  code
}

定义 methodName 作为运行的方法,在每个测试类创建。在测试类范围内,TEST_CLASS_INITIALIZE 在测试类只定义一次,因此必须定义。

TEST_CLASS_CLEANUP(methodName) 
{
    // test class cleanup  code
}

定义 methodName 作为运行的方法,在每个测试类创建。在测试类范围内,TEST_CLASS_CLEANUP 在测试类只定义一次,因此必须定义。

测试模块

TEST_MODULE_INITIALIZE(methodName)
{
    // module initialization code
}

当模块加载时,定义运行的方法 methodName。TEST_MODULE_INITIALIZE 在测试模块一次仅定义,并且声明必须在命名空间范围内。

TEST_MODULE_CLEANUP(methodName)

当模块不加载时,定义运行的方法 methodName。TEST_MODULE_CLEANUP 在测试模块一次仅定义,并且声明必须在命名空间范围内。

创建测试特性

TestMethod Attribute — TestMethod 特性

BEGIN_TEST_METHOD_ATTRIBUTE(testMethodName) 
    TEST_METHOD_ATTRIBUTE(attributeName, attributeValue)
    ...
END_TEST_METHOD_ATTRIBUTE()

添加特性定义带有一个或多个 TEST_METHOD_ATTRIBUTE 宏。测试方法 testClassName。

TEST_METHOD_ATTRIBUTE 宏定义具有名为 attributeName 和 attributeValue的特性。

TestClass Attribute — TestClass 特性

BEGIN_TEST_CLASS_ATTRIBUTE(testClassName) 
    TEST_CLASS_ATTRIBUTE(attributeName, attributeValue)
    ...
END_TEST_CLASS_ATTRIBUTE()

添加特性定义带有一个或多个 TEST_CLASS_ATTRIBUTE 宏。测试类 testClassName。

TEST_CLASS_ATTRIBUTE 宏定义具有名为 attributeName 和 attributeValue的特性。

模块测试特性

BEGIN_TEST_MODULE_ATTRIBUTE(testModuleName) 
    TEST_MODULE_ATTRIBUTE(attributeName, attributeValue)
    ...
END_TEST_MODULE_ATTRIBUTE()

添加特性定义带有一个或多个 TEST_MODULE_ATTRIBUTE 宏。测试模块 testModuleName。

TEST_MODULE_ATTRIBUTE 宏定义具有名为 attributeName 和 attributeValue的特性。

预定义特性

这些预定义特性宏可以替换宏所述的 TEST_METHOD_ATTRIBUTE、TEST_CLASS_ATTRIBUTE或 TEST_MODULE_ATTRIBUTE 将重写上面。

TEST_OWNER(ownerAlias)

定义使用名称 Owner 和属性值的特性ownerAlias。

TEST_DESCRIPTION(description)

定义使用名称 Description 和属性值的特性description。

TEST_PRIORITY(priority)

定义使用名称 Priority 和属性值的特性priority。

TEST_WORKITEM(workitem)

定义使用名称 WorkItem 和属性值的特性workItem。

TEST_IGNORE()

定义使用名称 Ignore 和属性值的特性true。

CppUnitTestAssert.h

泛断言

相等

验证两个对象相等

template<typename T> 
static void AreEqual(
    const T& expected, 
    const T& actual, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证两个相等

static void AreEqual(
    double expected, 
    double actual, 
    double tolerance, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证两个浮点型相等

static void AreEqual(
    float expected, 
    float actual, 
    float tolerance, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证两 char* 字符串相等

static void AreEqual(
    const char* expected, 
    const char* actual, 
    bool ignoreCase = false, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证两 w_char* 字符串相等

static void AreEqual(
    const wchar_t* expected, 
    const wchar_t* actual, 
    bool ignoreCase = false, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

不相等

验证两个不相等

static void AreNotEqual(
    double notExpected, 
    double actual, 
    double tolerance, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证两个浮点型不相等

static void AreNotEqual(
    float notExpected, 
    float actual, 
    float tolerance, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证两 char* 字符串不相等

static void AreNotEqual(
    const char* notExpected, 
    const char* actual, 
    bool ignoreCase = false, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证两 w_char* 字符串不相等

static void AreNotEqual(
    const wchar_t* notExpected, 
    const wchar_t* actual, 
    bool ignoreCase = false, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

验证基于 operator== 不相等两个引用。

template<typename T> 
static void AreNotEqual(
    const T& notExpected, 
    const T& actual, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

相同

验证两个引用所引用的是同一对象实例 (标识)。

template<typename T> 
static void AreSame(
    const T& expected, 
    const T& actual, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

不相同

验证两个引用所引用的不是同一对象实例 (标识)。

template<typename T> 
static void AreNotSame (
    const T& notExpected, 
    const T& actual, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

为 null。

验证指针为 NULL。

template<typename T> 
static void IsNull(
    const T* actual,
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

不为空

验证不为 NULL 指针

template<typename T> 
static void IsNotNull(
    const T* actual, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

为 true.

验证条件为 true

static void IsTrue(
    bool condition, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

为 false

验证条件为 false

static void IsFalse(
    bool condition, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

Fail

强制将失败的测试用例结果

static void Fail(
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)

Windows 运行时断言

相等

验证两个 Windows 运行时指针相同。

template<typename T> 
static void AreEqual(
    T^ expected, 
    T^ actual, 
    Platform::String^ message = nullptr, 
    const __LineInfo* pLineInfo= nullptr)

验证 Platform::String^ 两个字符串相等。

template<typename T> 
static void AreEqual(
    T^ expected, 
    T^ actual, 
    Platform::String^ message= nullptr, 
    const __LineInfo* pLineInfo= nullptr)

相同

验证 Windows 运行时两对引用同一对象。

template<typename T> 
static void AreSame(
    T% expected, 
    T% actual, 
    Platform::String^ message= nullptr, 
    const __LineInfo* pLineInfo= nullptr)

不相等

验证两个 Windows 运行时指针不相同。

template<typename T> 
static void AreNotEqual(
    T^ notExpected, 
    T^ actual, 
    Platform::String^ message = nullptr, 
    const __LineInfo* pLineInfo= nullptr)

验证 Platform::String^ 两个字符串不相等。

static void AreNotEqual(
    Platform::String^ notExpected, 
    Platform::String^ actual, 
    bool ignoreCase = false, 
    Platform::String^ message= nullptr, 
    const __LineInfo* pLineInfo= nullptr)

不相同

验证 Windows 运行时两对引用不同一对象。

template<typename T> 
static void AreNotSame(
    T% notExpected, 
    T% actual, 
    Platform::String^ message= nullptr, 
    const __LineInfo* pLineInfo= nullptr)

为 null。

验证 Windows 运行时是 nullptr 指针。

template<typename T> 
static void IsNull(
    T^ actual,
    Platform::String^ message = nullptr, 
    const __LineInfo* pLineInfo= nullptr)

不为空

验证 Windows 运行时不是 nullptr 指针。

template<typename T> 
static void IsNotNull(
    T^ actual, 
    Platform::String^ message= nullptr, 
    const __LineInfo* pLineInfo= nullptr)

Assert 异常

预期异常

验证函数引发异常:

template<typename _EXPECTEDEXCEPTION, typename _FUNCTOR> 
static void ExpectException(
    _FUNCTOR functor, 
    const wchar_t* message= NULL, 
    const __LineInfo* pLineInfo= NULL)

验证函数引发异常:

template<typename _EXPECTEDEXCEPTION, typename _RETURNTYPE> 
    static void ExpectException(
    _RETURNTYPE (*func)(), 
    const wchar_t* message= NULL, 
    const __LineInfo* pLineInfo = NULL)

CppUnitTestLogger.h

/logger:

记录器类包含静态方法。

class Logger

写消息

static void 
Logger::WriteMessage(const wchar_t* message)

static void 
Logger::WriteMessage(const char* message)

示例

这就是示例的代码。

////////////////////////////////////////////////////////////
/* USAGE EXAMPLE
// The following is an example of VSCppUnit usage.
// It includes examples of attribute metadata, fixtures,
// unit tests with assertions, and custom logging.

#include <CppUnitTest.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

BEGIN_TEST_MODULE_ATTRIBUTE()
    TEST_MODULE_ATTRIBUTE(L"Date", L"2010/6/12")
END_TEST_MODULE_ATTRIBUTE()

TEST_MODULE_INITIALIZE(ModuleInitialize)
{
    Logger::WriteMessage("In Module Initialize");
}

TEST_MODULE_CLEANUP(ModuleCleanup)
{
    Logger::WriteMessage("In Module Cleanup");
}

TEST_CLASS(Class1)
{

public:

    Class1()
    {
        Logger::WriteMessage("In Class1");
    }

    ~Class1()
    {
        Logger::WriteMessage("In ~Class1");
    }

    TEST_CLASS_INITIALIZE(ClassInitialize)
    {
        Logger::WriteMessage("In Class Initialize");
    }

    TEST_CLASS_CLEANUP(ClassCleanup)
    {
        Logger::WriteMessage("In Class Cleanup");
    }
    
    BEGIN_TEST_METHOD_ATTRIBUTE(Method1)
        TEST_OWNER(L"OwnerName")
        TEST_PRIORITY(1)
    END_TEST_METHOD_ATTRIBUTE()

    TEST_METHOD(Method1)
    {   
        Logger::WriteMessage("In Method1");
        Assert::AreEqual(0, 0);
    }

    TEST_METHOD(Method2)
    {
        Assert::Fail(L"Fail");
    }
};

请参见

概念

使用单元测试验证代码

用测试资源管理器对现有的 C++ 应用程序进行单元测试

其他资源

用测试资源管理器对本机代码进行单元测试