IT story

Visual Studio C ++에 대한 단위 테스트를 설정하는 방법

hot-time 2020. 9. 1. 08:03
반응형

Visual Studio C ++에 대한 단위 테스트를 설정하는 방법


기본 제공 단위 테스트 제품군을 사용하여 C ++ 용 Visual Studio 2008에서 테스트 프레임 워크를 설정하고 사용할 수있는 방법을 파악하는 데 문제가 있습니다.

모든 링크 또는 자습서를 주시면 감사하겠습니다.


이 페이지 는 도움 될 수 있으며, 몇 가지 C ++ 단위 테스트 프레임 워크를 검토합니다.

  • CppUnit
  • Boost.Test
  • CppUnitLite
  • NanoCppUnit
  • 단위 ++
  • CxxTest

CPPUnitLite 또는 CPPUnitLite2를 확인하십시오 .

CPPUnitLite 는 원래 Java의 JUnit을 CPPUnit으로 C ++로 포팅 한 Michael Feathers에 의해 만들어졌습니다.

CPPUnitLite는 C ++로 포팅 된 Java가 아닌 진정한 C ++ 스타일의 테스트 프레임 워크를 만들려고합니다. (나는 Feather 's Working Effectively with Legacy Code에서 의역했습니다 .) CPPUnitLite2 는 더 많은 기능과 버그 수정이있는 또 다른 재 작성 인 것 같습니다.

또한 CPPUnitLite2 및 기타 프레임 워크의 내용을 포함하는 UnitTest ++우연히 발견했습니다 .

Microsoft는 WinUnit 을 출시 했습니다 .

체크 아웃 Catch 또는 Doctest


Visual Studio 2008 내의 기본 제공 테스트 프레임 워크를 사용하여 관리되지 않는 C ++를 테스트하는 방법이 있습니다 . C ++ / CLI를 사용하여 C ++ 테스트 프로젝트를 만드는 경우 관리되지 않는 DLL을 호출 할 수 있습니다. 관리되지 않는 C ++로 작성된 코드를 테스트하려면 공용 언어 런타임 지원을 / clr : safe에서 / clr로 전환해야합니다.

내 블로그에 대한 단계별 세부 정보가 있습니다. http://msujaws.wordpress.com/2009/05/06/unit-testing-mfc-with-mstest/


다음은 Microsoft에서 IIS URL 재 작성 모듈을 테스트하는 데 사용하는 접근 방식입니다 (명령 줄 기반이지만 VS에서도 작동해야 함).

  1. 소스 코드를 cpp 파일로 이동하고 필요한 경우 앞으로 선언을 사용하여 헤더 파일이 사용 가능한지 확인하십시오.
  2. 코드를 컴파일하여 라이브러리 (.lib)로 테스트합니다.
  3. CLR 지원을 사용하여 UnitTest 프로젝트를 C ++로 만듭니다.
  4. 헤더 파일을 포함하십시오.
  5. .lib 파일을 포함합니다.
  6. Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll에 대한 참조를 추가합니다.
  7. 단위 테스트를 선언하기 위해 정말 작은 클래스를 사용하고 다음과 같이 관리되는 코드에서 C ++ / 네이티브 코드로 이동합니다 (오타가있을 수 있음).

다음은 예입니다.

// Example
#include "stdafx.h"
#include "mstest.h"

// Following code is native code.
#pragma unmanaged
void AddTwoNumbersTest() {
  // Arrange
  Adder yourNativeObject;
  int expected = 3;
  int actual;
  // Act
  actual = yourNativeObject.Add(1, 2);
  // Assert
  Assert::AreEqual(expected, actual, L"1 + 2 != 3");
}

// Following code is C++/CLI (Managed)
#pragma managed
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
[TestClass]
public ref class TestShim {
public:
  [TestMethod]
  void AddTwoNumbersTest() {
     // Just jump to C++ native code (above)
     ::AddTwoNumbersTest();
  }
};

이 접근 방식을 사용하면 사람들이 C ++ / CLI를 너무 많이 배울 필요가 없으며 모든 실제 테스트는 C ++ 네이티브로 수행되며 TestShim 클래스는 테스트를 MSTest.exe에 '게시'하거나 표시하는 데 사용됩니다. ).

새 테스트를 추가하려면 새 [TestMethod] void NewTest () {:: NewTest ();} 메서드와 새 void NewTest () 네이티브 함수를 선언하면됩니다. 매크로도, 트릭도, 전진합니다.

Now, the heade file is optionally, but it can be used to expose the Assert class' methods with C++ native signatures (e.g. wchar_t* instead of Stirng^), so it can you can keep it close to C++ and far from C++/CLI:

Here is an example:

// Example
#pragma once
#pragma managed(push, on)
using namespace System;
class Assert {
public:
    static void AreEqual(int expected, int actual) {
        Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual);
    }

    static void AreEqual(int expected, int actual, PCWSTR pszMessage) {
        Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual, gcnew String(pszMe
ssage));
    }

    template<typename T>
    static void AreEqual(T expected, T actual) {
        Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual);
    }

    // Etcetera, other overloads...
}
#pragma managed(pop)

HTH


Personally, I prefer WinUnit since it doesn't require me to write anything except for my tests (I build a .dll as the test, not an exe). I just build a project, and point WinUnit.exe to my test output directory and it runs everything it finds. You can download the WinUnit project here. (MSDN now requires you to download the entire issue, not the article. WinUnit is included within.)


The framework included with VS9 is .NET, but you can write tests in C++/CLI, so as long as you're comfortable learning some .NET isms, you should be able to test most any C++ code.

boost.test and googletest look to be fairly similar, but adapted for slightly different uses. Both of these have a binary component, so you'll need an extra project in your solution to compile and run the tests.

The framework we use is CxxTest, which is much lighter; it's headers only, and uses a Perl (!) script to scrape test suite information from your headers (suites inherit from CxxTest::Base, all your test methods' names start with "test"). Obviously, this requires that you get Perl from one source or another, which adds overhead to your build environment setup.


I use UnitTest++.

In the years since I made this post the source has moved from SourceForge to github. Also the example tutorial is now more agnostic - doesn't go into any configuration or project set up at all.

I doubt it will still work for Visual Studio 6 as the project files are now created via CMake. If you still need the older version support you can get the last available version under the SourceForge branch.


The tools that have been mentioned here are all command-line tools. If you look for a more integrated solution, have a look at cfix studio, which is a Visual Studio AddIn for C/C++ unit testing. It is quite similar to TestDriven.Net, but for (unmanaged) C/C++ rather than .NET.


I've used CppUnit with VS2005 and Eclipse. The wiki is very thorough (especially if you are familiar with JUnit).


I'm not 100% sure about VS2008, but I know that the Unit Testing framework that microsoft shipped in VS2005 as part of their Team Suite was only for .NET, not C++

I've used CppUnit also and it was alright. Much the same as NUnit/JUnit/so on.

If you've used boost, they also have a unit testing library

The guys behind boost have some serious coding chops, so I'd say their framework should be pretty good, but it might not be the most user friendly :-)


I like the CxxTest as well for the same reasons. It's a header file only so no linking required. You aren't stuck with Perl as there is a Python runner as well. I will be reviewing the google library soon. The Boost stuff pulls in too much other baggage.


The unit tester for Visual Studio 2008 is only for .NET code as far as I know.

I used CppUnit on Visual Studio 2005 and found it to be pretty good.

As far as I remember, the setup was relatively painless. Just make sure that in your testing projects the linker (Linker → Input → Additional Dependencies) includes cppunitd.lib.

Then, #include <cppunit/extensions/HelperMacros.h> in your header.

You can then follow the steps in http://cppunit.sourceforge.net/doc/1.11.6/cppunit_cookbook.html to get your test class working.


I was suffering to implement unit testing for an unmanaged C++ application in a Windows environment with Visual Studio. So I managed to overcome and wrote a post as a step-by-step guidance to unmanaged C++ application unit testing. I hope it may help you.

Unit test for unmanaged C++ in Visual Studio

참고URL : https://stackoverflow.com/questions/3150/how-to-set-up-unit-testing-for-visual-studio-c

반응형