Microsoft MVP성태의 닷넷 이야기
VC++: 117. Visual Studio - ATL COM 개체를 단위 테스트 하는 방법 [링크 복사], [링크+제목 복사],
조회: 9657
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

Visual Studio - ATL COM 개체를 단위 테스트 하는 방법

ATL COM DLL의 경우, 직접 COM 객체를 CoCreateInstance로 생성하는 것도 가능하지만 소스 코드를 단위 테스트 프로젝트에 추가하는 방법도 고려해 볼 수 있습니다. (이런 경우, Code Coverage 수치는 안 나옵니다.)

예를 들어, "TestComObject"라는 ATL COM 개체를 만들었다면 다음과 같이 해당 Header 파일을 include하고 소스 코드를 포함시킨 다음, CComObject로 생성할 수 있습니다.

#include "stdafx.h"
#include "CppUnitTest.h"

#include "..\..\dllmain.h"
#include "..\..\TestComObject.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace CppNativeTest
{
    TEST_CLASS(Test_TestComObject)
    {
    public:
        TEST_METHOD(Test_TestComObject_Initialize)
        {
            CComObject<CTestComObject> *inst;
            HRESULT hRes = CComObject<CTestComObject>::CreateInstance(&inst);
        }
    };
}

CComObject를 사용하는 이유는, ATL COM Class의 경우 추상 함수를 포함하고 있기 때문입니다. 그런데, 이렇게 해도 단위 테스트를 실행하면 다음과 같은 오류가 발생합니다.

Test Name:  Test_TestComObject_Initialize
Test FullName:  CppNativeTest::Test_TestComObject::Test_TestComObject_Initialize
Test Source:    d:\unittest\cppnativetest\TestComObjecttest.cpp : line 13
Test Outcome:   Failed
Test Duration:  0:00:00.1421573

Result StackTrace:  
at ATL::CComObject<CTestComObject>::CComObject<CTestComObject>() in c:\program files (x86)\microsoft visual studio 14.0\vc\atlmfc\include\atlcom.h:line 2908
    at ATL::CComObject<CTestComObject>::CreateInstance() in c:\program files (x86)\microsoft visual studio 14.0\vc\atlmfc\include\atlcom.h:line 2966
    at CppNativeTest::Test_TestComObject::Test_TestComObject_Initialize() in d:\unittest\cppnativetest\TestComObjecttest.cpp:line 17
Result Message: Exception Code: C0000005

문제가 발생한 atlcom.h의 소스 코드를 확인해 보면 _pAtlModule 포인터 값이 nullptr임을 알 수 있습니다.

// ===== atlcom.h
// ...[생략]...
template <class Base>
class CComObject : 
    public Base
{
public:
    typedef Base _BaseClass;
    CComObject(_In_opt_ void* = NULL)
    {
        _pAtlModule->Lock();
    }
    // ...[생략]...
}

// ...[생략]...

이 문제를 해결하려면 역시 ATL COM 프로젝트에 있던 Module 클래스를 맞춰주면 됩니다.

#include "..\..\TestComObject\dllmain.h"

TEST_METHOD(Test_TestComObject_Initialize)
{
    CTestComObjectModule _AtlModule;
    _pAtlModule = &_AtlModule;
    CComObject<CTestComObject> *inst;

    HRESULT hRes = CComObject<CTestComObject>::CreateInstance(&inst);
}

이렇게 바꾸고 컴파일하면 이번엔 빌드 오류가 나는데요.

Error LNK2001 unresolved external symbol LIBID_TestComObjectLib

이 값은 [...]_i.c 파일을 단위 테스트 프로젝트에 include 시켜주면 됩니다.

#include "..\..\TestComObject\TestComObject_i.c"



이외에도 단위 테스트 용도의 export 함수들을 ATL DLL에 포함시킨 다음 그것을 통해 COM 클래스를 호출하는 식으로도 고려해 볼 수 있습니다. 이런 경우 Code Coverage까지 모두 계산되어 좋습니다. 대신 출시 버전에 단위 테스트 함수들이 DLL에 포함되어 있는 것은 좋지 않기 때문에 Debug 빌드에만 적용하는 식이어야 할 것입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 4/5/2017]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  69  70  71  [72]  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
11839정성태3/12/201913980디버깅 기술: 124. .NET Core 웹 앱을 호스팅하는 Azure App Services의 프로세스 메모리 덤프 및 windbg 분석 개요 [3]
11838정성태3/7/201916755.NET Framework: 811. (번역글) .NET Internals Cookbook Part 1 - Exceptions, filters and corrupted processes [1]파일 다운로드1
11837정성태3/6/201926371기타: 74. 도서: 시작하세요! C# 7.3 프로그래밍 [10]
11836정성태3/5/201914326오류 유형: 525. Visual Studio 2019 Preview 4/RC - C# 8.0 Missing compiler required member 'System.Range..ctor' [1]
11835정성태3/5/201914071.NET Framework: 810. C# 8.0의 Index/Range 연산자를 .NET Framework에서 사용하는 방법 및 비동기 스트림의 컴파일 방법 [3]파일 다운로드1
11834정성태3/4/201912963개발 환경 구성: 432. Visual Studio 없이 최신 C# (8.0) 컴파일러를 사용하는 방법
11833정성태3/4/201913721개발 환경 구성: 431. Visual Studio 2019 - CMake를 이용한 공유/실행(so/out) 리눅스 프로젝트 설정파일 다운로드1
11832정성태3/4/201910766오류 유형: 524. Visual Studio CMake - rsync: connection unexpectedly closed
11831정성태3/4/201910320오류 유형: 523. Visual Studio 2019 - 새 창으로 뜬 윈도우를 닫을 때 비정상 종료
11830정성태2/26/201910162오류 유형: 522. 이벤트 로그 - Error opening event log file State. Log will not be processed. Return code from OpenEventLog is 87.
11829정성태2/26/201912049개발 환경 구성: 430. 마이크로소프트의 CoreCLR 프로파일러 예제 빌드 방법 - 리눅스 환경 [1]
11828정성태2/26/201918447개발 환경 구성: 429. Component Services 관리자의 RuntimeBroker 설정이 2개 있는 경우 [8]
11827정성태2/26/201912378오류 유형: 521. Visual Studio - Could not start the 'rsync' command on the remote host, please install it using your system package manager.
11826정성태2/26/201912267오류 유형: 520. 우분투에 .NET Core SDK 설치 시 패키지 의존성 오류
11825정성태2/25/201917117개발 환경 구성: 428. Visual Studio 2019 - CMake를 이용한 리눅스 빌드 환경 설정 [1]
11824정성태2/25/201911917오류 유형: 519. The SNMP Service encountered an error while accessing the registry key SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration. [1]
11823정성태2/21/201913381오류 유형: 518. IIS 관리 콘솔이 뜨지 않는 문제
11822정성태2/20/201911539오류 유형: 517. docker에 설치한 MongoDB 서버로 연결이 안 되는 경우
11821정성태2/20/201912009오류 유형: 516. Visual Studio 2019 - This extension uses deprecated APIs and is at risk of not functioning in a future VS update. [1]
11820정성태2/20/201915123오류 유형: 515. 윈도우 10 1809 업데이트 후 "User Profiles Service" 1534 경고 발생
11819정성태2/20/201913794Windows: 158. 컴퓨터와 사용자의 SID(security identifier) 확인 방법
11818정성태2/20/201912710VS.NET IDE: 131. Visual Studio 2019 Preview의 닷넷 프로젝트 빌드가 20초 이상 걸리는 경우 [2]
11817정성태2/17/20199907오류 유형: 514. WinDbg Preview 실행 오류 - Error : DbgX.dll : WindowsDebugger.WindowsDebuggerException: Could not load dbgeng.dll
11816정성태2/17/201912380Windows: 157. 윈도우 스토어 앱(Microsoft Store App)을 명령행에서 직접 실행하는 방법
11815정성태2/14/201911274오류 유형: 513. Visual Studio 2019 - VSIX 설치 시 "The extension cannot be installed to this product due to prerequisites that cannot be resolved." 오류 발생
11814정성태2/12/201910072오류 유형: 512. VM(가상 머신)의 NT 서비스들이 자동 시작되지 않는 문제
... 61  62  63  64  65  66  67  68  69  70  71  [72]  73  74  75  ...