2009-09-18 3 views
3

관리되지 않는 MFC 프로젝트를 테스트하기 위해 관리되는 C++ 단위 테스트 프로젝트를 만들려고합니다. 나는 msujaws의 절차를 읽고 그것을 따라 갔다. 그래서 같은 함수의 반환 문자열을 테스트하는 시험 방법을 구현 :이 코드를 컴파일 할 때, 그러나Visual Studio 2008의 관리되지 않는 C++ 단위 테스트

#include "stdafx.h" 
#include "TxStats.h" 

TxStats::TxStats() 
{ 
} 
/* 
This method returns a data rate string formatted in either Bytes, KBytes, MBytes or GBytes per sec 
from an int of the bytes per second. 
*/ 
CString TxStats::GetTxRateStr(__int64 Bps) 
{ 
enum DataUnits dunit; 
const __int64 dataSizes[]= { 0x1,  // 2^0 
     0x400,  // 2^10 
     0x100000, // 2^20 
     0x40000000};// 2^30 
const char *dataStrs[] = { "B/s", 
      "KB/s", 
      "MB/s", 
      "GB/s"}; 
CString out; 
double datarate; 
bool finish = false; 
for (dunit = A_KBYTE; dunit <= LARGER_THAN_BIGGEST_UNIT; dunit = DataUnits(dunit+1)) 
{ 
    if (dunit == LARGER_THAN_BIGGEST_UNIT) 
    { 
    if (dataSizes[dunit - 1] <= Bps) 
    { 
    //Gigabytes/sec 
    datarate = Bps/((double) dataSizes[dunit - 1]); 
    out.Format("%4.2f %s", datarate, dataStrs[dunit - 1]); 
    finish = true; 
    break; 
    } 
    } 
    else 
    { 
    if (Bps < dataSizes[dunit]) 
    { 
    //(Kilo, Mega)bytes/sec 
    datarate = Bps/((double) dataSizes[dunit - 1]); 
    out.Format("%4.2f %s", datarate, dataStrs[dunit - 1]); 
    finish = true; 
    break; 
    } 
    } 
} 
if (! finish) 
{ 
    out.Format("%s", "Unknown!"); 
} 
return out.GetBuffer(); 
} 


void TxStats::BytesToSizeStr(__int64 bytes, CString &out) 
{ 
if (bytes < 0) 
{ 
    out = "Err"; 
} 
else if (bytes == 0) 
{ 
    out = "0B"; 
} 
else 
{ 
    CString size; 
    CString byteChar = "B"; 
    CString unit; 
    int val; 
    if (bytes < 1024) 
    { 
    //Bytes 
    unit = ""; 
    val = (int)bytes; 
    } 
    else if ((bytes >> 10) < 1024) 
    { 
    //Kilobytes 
    unit = "K"; 
    __int64 div = 1 << 10; 
    val = (int) (bytes/((double) div)); 
    } 
    else if ((bytes >> 20) < 1024) 
    { 
    //Megabytes 
    unit = "M"; 
    __int64 div = 1 << 20; 
    val = (int) (bytes/((double) div)); 
    } 
    else 
    { 
    //Else assume gigabytes 
    unit = "G"; 
    __int64 div = 1 << 30; 
    val = (int) (bytes/((double) div)); 
    } 
    unit = unit + byteChar; 
    const char * unitCharBuf = unit.GetBuffer(); 
    size.Format("%d%s", ((int) val), unitCharBuf); 
    out = size.GetBuffer(); 
} 

} 

: 다음과 같다 다른 프로젝트의 코드를 테스트

#include "stdafx.h" 
#include "TxStats.h" 
#include <cstdlib> 
#include <atlstr.h> 

#pragma managed 

#using <mscorlib.dll> 
#using <System.dll> 
#using <system.data.dll> 

using namespace std; 
using namespace System; 
using namespace System::Text; 
using namespace System::Text::RegularExpressions; 
using namespace System::Collections::Generic; 
using namespace System::Runtime::InteropServices; 
using namespace Microsoft::VisualStudio::TestTools::UnitTesting; 

namespace AUnitTest 
{ 
    [TestClass] 
    public ref class TxStatsTest 
    { 
    private: 
     TestContext^ testContextInstance; 

    public: 
     /// <summary> 
     ///Gets or sets the test context which provides 
     ///information about and functionality for the current test run. 
     ///</summary> 
     property Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ TestContext 
     { 
      Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ get() 
      { 
       return testContextInstance; 
      } 
      System::Void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ value) 
      { 
       testContextInstance = value; 
      } 
     }; 

     #pragma region Additional test attributes 
     // 
     //You can use the following additional attributes as you write your tests: 
     // 
     //Use ClassInitialize to run code before running the first test in the class 
     //[ClassInitialize()] 
     //static void MyClassInitialize(TestContext^ testContext) {}; 
     // 
     //Use ClassCleanup to run code after all tests in a class have run 
     //[ClassCleanup()] 
     //static void MyClassCleanup() {}; 
     // 
     //Use TestInitialize to run code before running each test 
     //[TestInitialize()] 
     //void MyTestInitialize() {}; 
     // 
     //Use TestCleanup to run code after each test has run 
     //[TestCleanup()] 
     //void MyTestCleanup() {}; 
     // 
     #pragma endregion 

     [TestMethod] 
     void TestGetTxRateStr() 
     { 
      /* str to CString 

       CManagedClass* pCManagedClass = new CManagedClass(); 
       pCManagedClass->ShowMessage(strMessage); 

       char* szMessage = (char*)Marshal::StringToHGlobalAnsi(strMessage); 
       CUnmanagedClass cUnmanagedClass; cUnmanagedClass.ShowMessageBox(szMessage); 
       Marshal::FreeHGlobal((int)szMessage); 

      */ 
      CString out = TxStats::GetTxRateStr(1024); 
      // convert between MFC and .NET String implementations 
      String^myManagedString = Marshal::PtrToStringAnsi((IntPtr) (char *) out.GetBuffer()); 
      String^ret = myManagedString ;///gcnew String(); 
      Regex^matStr = gcnew Regex("1024 KB/s"); 
      StringAssert::Matches(ret, matStr); 
     } 
    }; 
} 

단위 테스트 프로젝트의 주요 프로젝트의 OBJ 파일에 연결되지 않는 이유

2>TxStatsTest.obj : error LNK2028: unresolved token (0A0005D4) "public: static class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > __cdecl TxStats::GetTxRateStr(__int64)" ([email protected]@@[email protected][email protected][email protected]@[email protected]@@@@[email protected]@[email protected]) referenced in function "public: void __clrcall AUnitTest::TxStatsTest::TestGetTxRateStr(void)" ([email protected]@[email protected]@$$FQ$AAMXXZ) 
2>TxStatsTest.obj : error LNK2019: unresolved external symbol "public: static class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > __cdecl TxStats::GetTxRateStr(__int64)" ([email protected]@@[email protected][email protected][email protected]@[email protected]@@@@[email protected]@[email protected]) referenced in function "public: void __clrcall AUnitTest::TxStatsTest::TestGetTxRateStr(void)" ([email protected]@[email protected]@$$FQ$AAMXXZ) 
2>\trunk\<proj>\Debug\AUnitTest.dll : fatal error LNK1120: 2 unresolved externals 
2>Caching metadata information for c:\program files\microsoft visual studio 9.0\common7\ide\publicassemblies\microsoft.visualstudio.qualitytools.unittestframework.dll... 
2>Build log was saved at "file://trunk\<proj>\AUnitTest\Debug\BuildLog.htm" 
2>AUnitTest - 3 error(s), 0 warning(s) 
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ========== 

사람이 제안 할 수 있습니다 : 나는 다음과 같은 오류가 발생합니다?

+0

제공된 답변 중 도움이 되셨습니까? – Jared

답변

3

당신은 다른 라이브러리와 링크하여 단위 테스트 프로젝트의 stdafx.cpp에

#pragma comment(lib, "TxStats.lib") 

을 추가 할 수 있습니다 (I 이미 단위 테스트 프로젝트의 종속성으로 주요 프로젝트를 지정).

+0

.lib하지만 .obj로 빌드되지 않은 경우 (또는 다른 프로젝트가 응용 프로그램이므로 그렇게 유지해야 함을 염두에두고 .lib로 변경할 수 있습니까?) – Jay

+1

다른 라이브러리에 영향을주지 않고 .lib 가져 오기 라이브러리를 출력 할 수 있습니다. 그냥 프로젝트의 링커 옵션의 고급 페이지에서 파일 이름을 지정하십시오. –

2

테스트 할 프로젝트의 * .obj 파일을 단위 테스트 프로젝트의 링커 입력에 추가해야합니다.

관련 문제