CoInitializeEx for boost :: test :: unit_test [英] CoInitializeEx for a boost::test::unit_test

查看:334
本文介绍了CoInitializeEx for boost :: test :: unit_test的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一天,我决定我需要知道在Windows平台上使用C ++的测试驱动开发(使用Visual Studio 2010 Premium)。

The other day, I decided that I needed to know about test driven development for C++ on the Windows platform (using Visual Studio 2010 Premium).

我在看看四周,然后决定尝试boost的单元测试框架。我应该说,我选择了boostpro.com的发布(目前是1.44,如果我记得正确)。这有一个静态库的构建,所以我不使用DLL在我的测试。

I had a look around before settling on trying out boost's unit test framework. I should say that I opted for boostpro.com's release (current is 1.44 if I recall correctly). This has a build of the static library, so I don't use the DLL in my tests.

Boost的单元测试文档谈论你的代码从测试套件,这似乎是合理的。但是,你必须处理从现在的独立测试套件项目引用你的代码的问题。

Boost's unit test documentation talks about seperating your code from your test suite, which seems reasonable. But then you must deal with the problem of referencing your code from your now seperate test suite project.

所以我有一个库项目,我想测试(但我' m仍然不知道如何写测试,可以引用一个.exe项目...)

So I have a library project that I want to test (but I'm still not sure how I'd write tests that can reference an .exe project...)

所以我在我的解决方案中创建了一个单独的项目称为单元测试。我添加了以下代码:

So I created a seperate project in my solution called unit tests. I added the following code:

#include "stdafx.h"

#define BOOST_TEST_MODULE Crash
#include <boost/test/unit_test.hpp> 
#include "LameEncoder.h"

BOOST_AUTO_TEST_SUITE(CrashTestSuite)

BOOST_AUTO_TEST_CASE(EncoderAvailable)
{
    using namespace Crash::SystemDevices::Audio::Compressors::LameEncoder;

    HRESULT hr = S_OK;
    CComPtr <IBaseFilter> spEncoder;

    hr = spEncoder.CoCreateInstance( CLSID_LAMEDShowFilter );
    if( spEncoder.p )
        spEncoder.Release();

    BOOST_CHECK_EQUAL( hr, S_OK );
}

BOOST_AUTO_TEST_CASE(ProfilesGenerated)
{
    using namespace Crash::SystemDevices::Audio::Compressors::LameEncoder;  
    BOOST_CHECK_EQUAL ( EncoderProfiles.size(), 6 );
}

BOOST_AUTO_TEST_SUITE_END()

崩溃库项目输出,然后我添加了以下后构建事件以获取报告后构建:

I statically link to my "crash" library project output, then I added the following post-build event to get a report post-build:

"$(TargetDir)\$(TargetName).exe" --result_code=no --report_level=short

后构建输出如下所示:

1>------ Build started: Project: UnitTests, Configuration: Debug Win32 ------
1>  UnitTests.cpp
1>  UnitTests.vcxproj -> F:\Projects\Crash\trunk\Debug\UnitTests.exe
1>  Running 2 test cases...
1>  f:/projects/crash/trunk/unittests/unittests.cpp(19): error in "EncoderAvailable": check hr == ((HRESULT)0L) failed [-2147221008 != 0]
1>  
1>  Test suite "Crash" failed with:
1>    1 assertion out of 2 passed
1>    1 assertion out of 2 failed
1>    1 test case out of 2 passed
1>    1 test case out of 2 failed

我预计EncoderAvailable测试失败,一个COM公寓的线程。我假设我不能使用自动测试,而是我需要替换自动测试用测试我手动定义自己在一个主要的函数,并在我的CoInitializeEx()调用在主函数。

I expected the EncoderAvailable test to fail, since I haven't initialized a COM apartment for the thread. I'd assume that I can't use auto tests, and instead I need to replace the auto tests with tests I manually define myself in a main function, and do my CoInitializeEx() calls in the main function.

我读过这里,你可以定义入口点并注册自己的函数,所以我给了这个去:

I've read here that you can define the entry point and register your own functions, so I gave this a go:

#include "stdafx.h"

#include <boost/test/unit_test.hpp>
using namespace boost::unit_test;

#include "LameEncoderTests.h"


test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    CoInitializeEx(NULL, COINIT_MULTITHREADED);

    framework::master_test_suite().
        add( BOOST_TEST_CASE( &LameEncoderAvailable ) );

    framework::master_test_suite().
        add( BOOST_TEST_CASE( &LameEncoderProfilesGenerated ) );

    CoUninitialize();

    return 0;
}

这里是build ouptut:

Here's the build ouptut:

    1>------ Build started: Project: UnitTests, Configuration: Debug Win32 ------
1>  UnitTests.cpp
1>  UnitTests.vcxproj -> F:\Projects\Crash\trunk\Debug\UnitTests.exe
1>  Running 2 test cases...
1>  f:/projects/crash/trunk/unittests/lameencodertests.h(17): error in "LameEncoderAvailable": check hr == ((HRESULT)0L) failed [-2147221008 != 0]
1>  
1>  Test suite "Master Test Suite" failed with:
1>    1 assertion out of 2 passed
1>    1 assertion out of 2 failed
1>    1 test case out of 2 passed
1>    1 test case out of 2 failed
1>  
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

第一个测试LameEncoderAvailable的测试失败失败,这是一个简单的函数:

That test failure fails on the first test LameEncoderAvailable, which is the following simple function:

void LameEncoderAvailable()
{
    using namespace Crash::SystemDevices::Audio::Compressors::LameEncoder;

    HRESULT                 hr              = S_OK;
    CComPtr<IBaseFilter>    spEncoder;

    hr = spEncoder.CoCreateInstance( CLSID_LAMEDShowFilter );
    if( spEncoder.p )
        spEncoder.Release();

    BOOST_CHECK_EQUAL( hr, S_OK );
}



<我不认为我应该这样做每次测试 - 每次线程只能做一次...

Can anyone tell me where the correct place to make the CoInitializeEx() call - I don't think I should be doing so once per test - it should only be done once per thread...

对于测试exe项目,我想你可以指定单独的main.cpp(testmain.cpp或某事),并从构建中排除您的真正main.cpp以访问您的代码。如果有人知道一个更优雅的解决方案,我会很想知道它...

As for testing exe projects, I guess you could specify a separate main.cpp (testmain.cpp or something) and exclude your real main.cpp from the build to access your code. If anyone knows of a more elegant solution to that one, I'd be keen to hear about it...

推荐答案

全球灯具。夹具是为每个测试设置初始化/关闭代码的好方法。全局灯具可以为您的整个测试套件定义初始化/关闭代码。

Use a Global Fixture. Fixtures are a great way to set up initialization/shutdown code for each test. A global fixture lets you define initialization/shutdown code for your whole test suite.

这篇关于CoInitializeEx for boost :: test :: unit_test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆