Microsoft MVP성태의 닷넷 이야기
VC++: 117. Visual Studio - ATL COM 개체를 단위 테스트 하는 방법 [링크 복사], [링크+제목 복사],
조회: 16051
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... 106  107  108  [109]  110  111  112  113  114  115  116  117  118  119  120  ...
NoWriterDateCnt.TitleFile(s)
11196정성태5/4/201720658오류 유형: 384. Msvm_ImageManagementService WMI 객체를 사용할 때 오류 상황 정리 [1]
11195정성태5/3/201720954.NET Framework: 655. .NET Framework 4.7 릴리스
11194정성태5/3/201723243오류 유형: 383. net use 명령어로 네트워크 드라이브 연결 시 "System error 67 has occurred." 오류 발생
11193정성태5/3/201721458Windows: 141. 설치된 Windows로부터 설치 이미지를 만드는 방법
11192정성태5/2/201721985Windows: 140. unattended.xml/autounattend.xml 파일을 마련하는 방법
11191정성태5/2/201722762Windows: 139. Dell Venue 8 Pro 태블릿에 USB를 이용한 윈도우 운영체제 설치 방법
11190정성태5/2/201728104Windows: 138. Windows 운영체제의 ISO 설치 파일에 미리 Device driver를 준비하는 방법
11189정성태5/2/201720118Windows: 137. Windows 7 USB/DVD DOWNLOAD TOOL로 98%에서 실패하는 경우
11188정성태4/27/201722649VC++: 118. Win32 HANDLE 자료형의 이모저모 [1]
11187정성태4/26/201723175개발 환경 구성: 314. C# - PowerPoint 확장 Add-in 만드는 방법 [1]파일 다운로드1
11186정성태4/24/201720945VS.NET IDE: 117. Visual Studio 확장(VSIX)을 이용해 사용자 매크로를 추가하는 방법 [1]파일 다운로드1
11185정성태4/22/201718912VS.NET IDE: 116. Visual Studio 확장(VSIX)을 이용해 사용자 메뉴 추가하는 방법 (2) - 동적 메뉴 구성파일 다운로드1
11184정성태4/21/201720424VS.NET IDE: 115. Visual Studio 확장(VSIX)을 이용해 사용자 메뉴 추가하는 방법파일 다운로드1
11183정성태4/19/201719218.NET Framework: 654. UWP 앱에서 FolderPicker 사용 시 유의 사항파일 다운로드1
11182정성태4/19/201723257개발 환경 구성: 313. Nuget Facebook 라이브러리를 이용해 ASP.NET 웹 폼과 로그인 연동하는 방법
11181정성태4/18/201720198개발 환경 구성: 312. Azure Web Role의 AppPool 실행 권한을 Local System으로 바꾸는 방법
11180정성태4/16/201723226Java: 18. Java의 Memory Mapped File 자원 반환이 안 되는 문제
11179정성태4/13/201716441기타: 64. SVG Converter 스토어 앱 개인정보 보호 정책 안내
11178정성태4/10/201718556개발 환경 구성: 311. COM+ 관리자의 DCOM 구성에 나오는 기준
11177정성태4/7/201719077.NET Framework: 653. C# 7 새로운 문법(1) - 더욱 편리해진 Out 변수 사용파일 다운로드1
11176정성태4/5/201716051VC++: 117. Visual Studio - ATL COM 개체를 단위 테스트 하는 방법
11175정성태4/5/201725767.NET Framework: 652. C# 개발자를 위한 C++ COM 객체의 기본 구현 방식 설명파일 다운로드1
11174정성태4/3/201719319VC++: 116. Visual Studio 단위 테스트 - Failed to set up the execution context to run the test
11173정성태4/3/201722939VC++: 115. Visual Studio에서 C++ DLL을 대상으로 단위 테스트할 때 비정상 종료한다면?파일 다운로드1
11172정성태4/3/201722059.NET Framework: 651. C# - 특정 EXE 프로세스를 종료시킨 EXE를 찾아내는 방법파일 다운로드1
11171정성태3/31/201718759VS.NET IDE: 114. Visual Studio 디버깅 경고 창 - You are debugging a Release build of ...
... 106  107  108  [109]  110  111  112  113  114  115  116  117  118  119  120  ...