简体   繁体   English

用C ++进行单元测试

[英]Unit tests in C++

I'm doing a project in C++ in my University and we need to unit test our classes. 我在我的大学里用C ++做一个项目,我们需要对我们的类进行单元测试。 The tests are pretty straight-forward - we don't have any "problematic" classes that deal with databases, GUI, web stuff, etc. It's just a command line program. 测试非常简单 - 我们没有任何“有问题”的类来处理数据库,GUI,Web东西等。它只是一个命令行程序。

What is a good unit-testing framework to use that is as simple as possible? 什么是一个好的单元测试框架,使用尽可能简单? Please provide a short example of a test in that framework. 请提供该框架中测试的简短示例。

EDIT : I see there are some answers, so I want to add another question: Where do I put the test methods? 编辑 :我看到有一些答案,所以我想补充一个问题:我在哪里放测试方法? Are they declared in a different file? 它们是在不同的文件中声明的吗? Where would that file be? 该文件在哪里? How do I run all tests? 我如何运行所有测试?

Boost. 促进。 Hands down. 把手放下。

#define BOOST_TEST_MODULE my_tests // use once per test program
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( case_x )
{
  ....
  BOOST_CHECK( ... boolean expression ... );
  BOOST_etc...etc...
}

There're many, quite similar. 有很多,非常相似。 My prefered one is Boost.Test library. 我最喜欢的是Boost.Test库。 It can be complicated if you need but also extremely simple for simple cases. 如果你需要它可能很复杂,但对于简单的情况也非常简单。 eg the simplest possible case looks like: 例如,最简单的案例如下:

#include <boost/test/minimal.hpp>

int add( int i, int j ) { return i+j; }

    int test_main( int, char *[] )             // note the name!
    {
        // six ways to detect and report the same error:
        BOOST_CHECK( add( 2,2 ) == 4 );        // #1 continues on error
        BOOST_REQUIRE( add( 2,2 ) == 4 );      // #2 throws on error
        if( add( 2,2 ) != 4 )
          BOOST_ERROR( "Ouch..." );            // #3 continues on error
        if( add( 2,2 ) != 4 )
          BOOST_FAIL( "Ouch..." );             // #4 throws on error
        if( add( 2,2 ) != 4 ) throw "Oops..."; // #5 throws on error

        return add( 2, 2 ) == 4 ? 0 : 1;       // #6 returns error code
    }

This example uses the Minimal Testing Facility . 此示例使用最小测试工具

Google Test is excellent. Google Test非常棒。 I like the actual writing of the tests a bit less than Boost (boost's UTF is EXCELLENT), but it does produce pretty console logs with colors and such on both Windows and most POSIX platforms. 我喜欢测试的实际写入比Boost少一点(boost的UTF非常好),但它确实在Windows和大多数POSIX平台上生成了带有颜色的漂亮控制台日志。

在使用C ++编程时, 可以很好地概述各种单元测试选项。

IMHO, UnitTest+ + is what you are looking for: 恕我直言, UnitTest + +正在寻找:

UnitTest++ is a lightweight unit testing framework for C++. UnitTest ++是一个用于C ++的轻量级单元测试框架。

It was designed to do test-driven development on a wide variety of platforms. 它旨在在各种平台上进行测试驱动的开发。 Simplicity, portability, speed, and small footprint are all very important aspects of UnitTest++. 简单性,可移植性,速度和小占用空间都是UnitTest ++的重要方面。

#include "stdafx.h"
#include "UnitTest++.h" 

TEST( HelloUnitTestPP )
{
   CHECK( false );
} 

int main( int, char const *[] )
{
   return UnitTest::RunAllTests();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM