[ロック解除] Catch2 - C ++テストフレームワーク(クイックスタート)

Catch2

Catch2はシンプルでC ++テストフレームワークである、とGTEST、boost.testとあなただけ簡単に使用できる1つのヘッダファイルが必要な場合でもに比べCppUnitのCatch2非常に小さいです。簡単な杭フレームと連動して、テストフレームワークを構築するためにそれを使用することができます小規模プロジェクトでスタブ、テストケースがアップ実行させるために毎分。
今日、我々は[ロック解除] Catch2に持っています。

得ます

Catch2を取得する方法は2つあります。
一つは直接ダウンロードヘッダであるcatch.hpp -あなたは、単にあなたのプロジェクトに統合することができ、このモードを使用することをお勧めします。
デモ内部の二次開発や学習に//github.com/catchorg/Catch2.git:他にはcatch2ソース、HTTPSを取得することです。

コンパイル

我々はCatch2今日いくつかの例を分析することによって、Catch2の使用のロックを解除したいので、彼らはソースコードをコンパイルしています。

ソースを取得します。1.

git clone https://github.com/catchorg/Catch2.git

コンパイラの例を回し2.
cmake -DCATCH_BUILD_EXAMPLES=ON ../

(base) frank@deepin:~/git/Catch2$ mkdir build
(base) frank@deepin:~/git/Catch2$ cd build/
(base) frank@deepin:~/git/Catch2/build$ cmake -DCATCH_BUILD_EXAMPLES=ON ../
-- The CXX compiler identification is GNU 9.2.0
-- Check for working CXX compiler: /usr/local/bin/c++
-- Check for working CXX compiler: /usr/local/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /home/frank/miniconda3/bin/python (found version "3.7.4") 
-- Examples included
-- Configuring done
-- Generating done
-- Build files have been written to: /home/frank/git/Catch2/build
(base) frank@deepin:~/git/Catch2/build$ make -j4

テスト

最も簡単な例で始まります。

#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

int Factorial( int number ) {
   return number <= 1 ? number : Factorial( number - 1 ) * number;  // fail
// return number <= 1 ? 1      : Factorial( number - 1 ) * number;  // pass
}

TEST_CASE( "Factorial of 0 is 1 (fail)", "[single-file]" ) {
    REQUIRE( Factorial(0) == 1 );
}

TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[single-file]" ) {
    REQUIRE( Factorial(1) == 1 );
    REQUIRE( Factorial(2) == 2 );
    REQUIRE( Factorial(3) == 6 );
    REQUIRE( Factorial(10) == 3628800 );
}

1行目:の#define CATCH_CONFIG_MAIN、このマクロ定義catch2の主な機能。

// Standard C/C++ main entry point
int main (int argc, char * argv[]) {
    return Catch::Session().run( argc, argv );
}

3行目:catch2ヘッダファイルを導入し、ここでは「<...>」で、使用がコンパイルしてインストールしcatch2後にメイクインストール実行され、システムディレクトリにインストールします。あなたが1つのヘッダーファイルを使用している場合、あなたは、「catch2.hpp」を使用する必要があり确认catch2.hpp、プロジェクトのディレクトリまたはヘッダファイルのディレクトリにそれを導入しました。

5行目:int型階乗(int型の数)は、関数を測定しています。

10、14行:2つのテストケースである
REQUIRE:アサートされます。

結果は以下の通りであります:


010-TestCase is a Catch v2.11.1 host application.
Run with -? for options

-------------------------------------------------------------------------------
Factorial of 0 is 1 (fail)
-------------------------------------------------------------------------------
/home/frank/git/Catch2/examples/010-TestCase.cpp:13
...............................................................................

/home/frank/git/Catch2/examples/010-TestCase.cpp:14: FAILED:
  REQUIRE( Factorial(0) == 1 )
with expansion:
  0 == 1

===============================================================================
test cases: 2 | 1 passed | 1 failed
assertions: 5 | 4 passed | 1 failed

独自のメインを書きます()

あなたは、メインCatch2は()関数を提供し使用しない場合、あなたはあなたのメインの自分の()を書くことができます。多くのプロジェクトは、多くの場合、あなたは以下の方法自体を使用することができ、main()関数を記述する必要があります。

#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

int main( int argc, char* argv[] ) {
  // global setup...

  int result = Catch::Session().run( argc, argv );

  // global clean-up...

  return result;
}

あなたがあなた自身のコマンドラインパラメータを使用したい場合は、達成することができます。

#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

int main( int argc, char* argv[] )
{
  Catch::Session session; // There must be exactly one instance
  
  int height = 0; // Some user variable you want to be able to set
  
  // Build a new parser on top of Catch's
  using namespace Catch::clara;
  auto cli 
    = session.cli() // Get Catch's composite command line parser
    | Opt( height, "height" ) // bind variable to a new option, with a hint string
        ["-g"]["--height"]    // the option names it will respond to
        ("how high?");        // description string for the help output
        
  // Now pass the new composite back to Catch so it uses that
  session.cli( cli ); 
  
  // Let Catch (using Clara) parse the command line
  int returnCode = session.applyCommandLine( argc, argv );
  if( returnCode != 0 ) // Indicates a command line error
      return returnCode;

  // if set on the command line then 'height' is now set at this point
  if( height > 0 )
      std::cout << "height: " << height << std::endl;

  return session.run();
}

セクション

あなたは全体のプロパティとしてクラスを設定する必要があるときは、クラスをテストするには、セットアップ()とteardow()方法を使用することができます。試験方法は、これらの特定のプロパティの各メンバーに基づいていますが、多くの場合、各メンバーがテストシナリオのさまざまな方法があります、あなたは便利SECTIONに来ることができます。
すなわち、実施形態のこのクラスの属性のセットが表示されることがあり、このTEST_CASE異なるシナリオ部材の切断法の範囲内で、TEST_CASEカテゴリを使用することができる、です。
次の例で説明しました:

#include <catch2/catch.hpp>

TEST_CASE( "vectors can be sized and resized", "[vector]" ) {

    // For each section, vector v is anew:

    std::vector<int> v( 5 );

    REQUIRE( v.size() == 5 );
    REQUIRE( v.capacity() >= 5 );

    SECTION( "resizing bigger changes size and capacity" ) {
        v.resize( 10 );

        REQUIRE( v.size() == 10 );
        REQUIRE( v.capacity() >= 10 );
    }
    SECTION( "resizing smaller changes size but not capacity" ) {
        v.resize( 0 );

        REQUIRE( v.size() == 0 );
        REQUIRE( v.capacity() >= 5 );
    }
    SECTION( "reserving bigger changes capacity but not size" ) {
        v.reserve( 10 );

        REQUIRE( v.size() == 5 );
        REQUIRE( v.capacity() >= 10 );
    }
    SECTION( "reserving smaller does not change size or capacity" ) {
        v.reserve( 0 );

        REQUIRE( v.size() == 5 );
        REQUIRE( v.capacity() >= 5 );
    }
}

:7行目std::vector<int> v( 5 );のカテゴリはカテゴリ(TEST_CASE)です。
最初の行13,19,25,31:resize()異なるシナリオの実施例は、方法(セクション)カテゴリを使用する方法。

そして、あなたは、特定のセクションの実行を指定するには、コマンドライン引数を使用することができます。

(base) frank@deepin:~/git/Catch2/build/examples$ ./100-Fix-Section -c "resizing bigger changes size and capacity" 
===============================================================================
All tests passed (4 assertions in 1 test case)

BDDスタイル

総合的かつSECTION似ていますが、近い自然言語や行動に、BDD(ビヘイビア駆動開発) -あなたは要件文書としてあなたのテストケースを置くことができます。
例:

#include <catch2/catch.hpp>

SCENARIO( "vectors can be sized and resized", "[vector]" ) {

    GIVEN( "A vector with some items" ) {
        std::vector<int> v( 5 );

        REQUIRE( v.size() == 5 );
        REQUIRE( v.capacity() >= 5 );

        WHEN( "the size is increased" ) {
            v.resize( 10 );

            THEN( "the size and capacity change" ) {
                REQUIRE( v.size() == 10 );
                REQUIRE( v.capacity() >= 10 );
            }
        }
        WHEN( "the size is reduced" ) {
            v.resize( 0 );

            THEN( "the size changes but not capacity" ) {
                REQUIRE( v.size() == 0 );
                REQUIRE( v.capacity() >= 5 );
            }
        }
        WHEN( "more capacity is reserved" ) {
            v.reserve( 10 );

            THEN( "the capacity changes but not the size" ) {
                REQUIRE( v.size() == 5 );
                REQUIRE( v.capacity() >= 10 );
            }
        }
        WHEN( "less capacity is reserved" ) {
            v.reserve( 0 );

            THEN( "neither size nor capacity are changed" ) {
                REQUIRE( v.size() == 5 );
                REQUIRE( v.capacity() >= 5 );
            }
        }
    }
}
リリース8元の記事 ウォンの賞賛1 ビュー161

おすすめ

転載: blog.csdn.net/xk_xx/article/details/104102831