{"id":2772,"date":"2018-03-29T23:46:21","date_gmt":"2018-03-29T21:46:21","guid":{"rendered":"http:\/\/mariusbancila.ro\/blog\/?p=2772"},"modified":"2020-06-26T21:53:35","modified_gmt":"2020-06-26T19:53:35","slug":"writing-cpp-unit-tests-with-catch2","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/","title":{"rendered":"Writing C++ unit tests with Catch2"},"content":{"rendered":"<p>In my book, <a href=\"https:\/\/www.packtpub.com\/application-development\/modern-c-programming-cookbook\">Modern C++ Programming Cookbook<\/a>, I discussed several testing frameworks for C++, more precisely, Boost.Test, Google Test, and Catch (which stands for <em>C++ Automated Test Cases in a Header<\/em>). Since the publishing of the book, a new version of Catch, called <a href=\"https:\/\/github.com\/catchorg\/Catch2\">Catch2<\/a> has been released. This provides new functionalities, but also a series of breaking changes with Catch, including the drop of support for pre-C++11 compilers. For a list of changes, you can see the <a href=\"https:\/\/github.com\/catchorg\/Catch2\/releases\/tag\/v2.0.1\">Catch2 release notes<\/a>. Catch2 is available as a single-header library, is open-sources and cross-platform, and written for C++11\/14\/17\/latest. In this article, I want to give a brief example of how you can write tests for C++ using Catch2.<br \/>\n<!--more--><\/p>\n<p>In order to do so, I will consider the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Fizz_buzz\">fizzbuzz<\/a> game. This is a numbers game for children; one child is supposed to say a number and the other has to answer with:<\/p>\n<ul>\n<li>fizz, if the number is divisible by 3,<\/li>\n<li>buzz, if the number if divisible by 5,<\/li>\n<li>fizzbuzz, if the number if divisible by both 3 and 5, or<\/li>\n<li>the number itself in all other cases.<\/li>\n<\/ul>\n<p>The function <tt>fizzbuzz()<\/tt> below implements this game:<\/p>\n<pre class=\"lang:c++ decode:true \" >#include &lt;string&gt;\r\n\r\nstd::string fizzbuzz(int const number)\r\n{\r\n   if (number != 0)\r\n   {\r\n      auto m3 = number % 3;\r\n      auto m5 = number % 5;\r\n      if (!m5 &amp;&amp; !m3) { return \"fizzbuzz\"; }\r\n      else if (!m5) { return \"buzz\"; }\r\n      else if (!m3) { return \"fizz\"; }\r\n   }\r\n   return std::to_string(number);\r\n}<\/pre>\n<p>This is what I want to test and the first test I would try is the result for number 0. In Catch2, you can write the test cases as follows:<\/p>\n<pre class=\"lang:c++ decode:true \" >TEST_CASE(\"Test with zero\", \"[classic]\")\r\n{\r\n   REQUIRE(fizzbuzz(0) == \"0\");\r\n}<\/pre>\n<p>The <tt>TEST_CASE<\/tt> macro defines a test case, called <tt>\"Test with zero\"<\/tt> here, and may associate tags to the case, such as <tt>[classic]<\/tt> in this example. Tags are used for selecting what test cases to run. <tt>REQUIRE<\/tt> is an assertion macro that determines the test to fail if the condition is not satisfied. There are several other assertion macros and you can read about them <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/assertions.md#top\">here<\/a>.<\/p>\n<p>Of course, we need more tests for this function, and the following test case called <tt>\"Test positives\"<\/tt> defines more:<\/p>\n<pre class=\"lang:default decode:true \" >TEST_CASE(\"Test positives\", \"[classic]\")\r\n{\r\n   SECTION(\"Test all up to 10\") {\r\n      REQUIRE(fizzbuzz(1) == \"1\"); \r\n      REQUIRE(fizzbuzz(2) == \"2\");\r\n      REQUIRE(fizzbuzz(3) == \"fizz\");\r\n      REQUIRE(fizzbuzz(4) == \"4\");\r\n      REQUIRE(fizzbuzz(5) == \"buzz\");\r\n      REQUIRE(fizzbuzz(6) == \"fizz\");\r\n      REQUIRE(fizzbuzz(7) == \"7\");\r\n      REQUIRE(fizzbuzz(8) == \"8\");\r\n      REQUIRE(fizzbuzz(9) == \"fizz\");\r\n      REQUIRE(fizzbuzz(10) == \"buzz\");\r\n   }\r\n\r\n   SECTION(\"Test all multiples of 3 only up to 100\") {\r\n      for (int i = 3; i &lt;= 100; i+=3) {\r\n         if (i % 5) REQUIRE(fizzbuzz(i) == \"fizz\");\r\n      }\r\n   }\r\n\r\n   SECTION(\"Test all multiples of 5 only up to 100\") {\r\n      for (int i = 5; i &lt;= 100; i += 5) {\r\n         if (i % 3) REQUIRE(fizzbuzz(i) == \"buzz\");\r\n      }\r\n   }\r\n\r\n   SECTION(\"Test all multiples of 3 and 5 up to 100\") {\r\n      for (int i = 15; i &lt;= 100; i += 15) {\r\n         REQUIRE(fizzbuzz(i) == \"fizzbuzz\");\r\n      }\r\n   }\r\n}<\/pre>\n<p>This is a little bit different than the test case above because it makes use of another macro called <tt>SECTION<\/tt>. This introduces a test function. Test functions can be nested (without limit) and they form a tree structure with test cases on root nodes and test function on inner and leaf nodes. When a leaf test functions is run, the entire code from the root test case to the leaf test function is executed. As a result, when multiple test functions (i.e. section) share common code, that code is executed for each section. This makes it unnecessary to have fixtures with setup and teardown code.<\/p>\n<p>Here is yet another test case, this time for negative numbers:<\/p>\n<pre class=\"lang:c++ decode:true \" >TEST_CASE(\"Test negatives\", \"[classic]\")\r\n{\r\n   REQUIRE(fizzbuzz(-1) == \"-1\");\r\n   REQUIRE(fizzbuzz(-2) == \"-2\");\r\n   REQUIRE(fizzbuzz(-3) == \"fizz\");\r\n   REQUIRE(fizzbuzz(-4) == \"-4\");\r\n   REQUIRE(fizzbuzz(-5) == \"buzz\");\r\n   REQUIRE(fizzbuzz(-6) == \"fizz\");\r\n   REQUIRE(fizzbuzz(-7) == \"-7\");\r\n   REQUIRE(fizzbuzz(-8) == \"-8\");\r\n   REQUIRE(fizzbuzz(-9) == \"fizz\");\r\n   REQUIRE(fizzbuzz(-10) == \"buzz\");\r\n}<\/pre>\n<p>Catch2 is automatically registering test cases and no additional work is necessary for that. Moreover, Catch2 can supply a <tt>main()<\/tt> function with all that is necessary for the setup of the framework. All you have to do for that is define the macro <tt>CATCH_CONFIG_MAIN<\/tt> before including the Catch2 header.<\/p>\n<pre class=\"lang:c++ decode:true \" >#define CATCH_CONFIG_MAIN\r\n#include \"catch.hpp\"<\/pre>\n<p>Of course, you can supply your own <tt>main()<\/tt> but in this case you need to call into Catch2 yourself. This, however, allows you to tweak the configuration or provide your own command line options. To supply your own implementation of <tt>main()<\/tt> you need to define the macro <tt>CATCH_CONFIG_RUNNER<\/tt>. You can read more about this options here: <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/own-main.md#top\">Supplying main() yourself<\/a>.<\/p>\n<p>You can execute the tests simply by running your application.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch1.png\" alt=\"\" width=\"547\" height=\"163\" class=\"alignnone size-full wp-image-2780\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch1.png 547w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch1-300x89.png 300w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/p>\n<p>If you have errors in your tests, this is how they are reported (to the console):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch4.png\" alt=\"\" width=\"893\" height=\"639\" class=\"alignnone size-full wp-image-2777\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch4.png 893w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch4-300x215.png 300w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch4-768x550.png 768w\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" \/><\/p>\n<p>There is a multitude of command line options, that allow you to configure what tests are running, how they are running, how the results are reported, etc. You can read about them here: <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/command-line.md#top\">Command line<\/a>. Here are some additional examples:<\/p>\n<ul>\n<li>Showing results for successful tests too (with arguments <tt>-s<\/tt>)\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch2.png\" alt=\"\" width=\"678\" height=\"693\" class=\"alignnone size-full wp-image-2779\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch2.png 678w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch2-294x300.png 294w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/p>\n<\/li>\n<li>Showing compact results, including for successful tests too (with arguments <tt>-s -r compact<\/tt>)\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch3.png\" alt=\"\" width=\"1083\" height=\"321\" class=\"alignnone size-full wp-image-2778\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch3.png 1083w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch3-300x89.png 300w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch3-768x228.png 768w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch3-1024x304.png 1024w\" sizes=\"auto, (max-width: 1083px) 100vw, 1083px\" \/><\/p>\n<\/li>\n<li>Showing results in a JUnit XML Report ANT format (with arguments <tt>-r junit<\/tt>)\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch5.png\" alt=\"\" width=\"938\" height=\"285\" class=\"alignnone size-full wp-image-2783\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch5.png 938w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch5-300x91.png 300w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch5-768x233.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><\/p>\n<\/li>\n<\/ul>\n<p>If you prefer to write your tests using a BDD approach, you can still do that with Catch2. The following are examples for testing the <tt>fizzbuzz()<\/tt> function.<\/p>\n<pre class=\"lang:c++ decode:true \" >SCENARIO(\"BDD test with zero\", \"[bdd]\")\r\n{\r\n   WHEN(\"The number is 0\") {\r\n      THEN(\"The result is 0\") {\r\n         REQUIRE(fizzbuzz(0) == \"0\");\r\n      }\r\n   }\r\n}\r\n\r\nSCENARIO(\"BDD test any number\", \"[bdd]\")\r\n{\r\n   GIVEN(\"Any positive number\")\r\n   {\r\n      WHEN(\"The number is 1\") {\r\n         THEN(\"The result is 1\") {\r\n            REQUIRE(fizzbuzz(1) == \"1\");\r\n         }\r\n      }\r\n      WHEN(\"The number is 2\") {\r\n         THEN(\"The result is 2\") {\r\n            REQUIRE(fizzbuzz(2) == \"2\");\r\n         }\r\n      }\r\n      WHEN(\"The number is 3\") {\r\n         THEN(\"The result is fizz\") {\r\n            REQUIRE(fizzbuzz(3) == \"fizz\");\r\n         }\r\n      }\r\n      WHEN(\"The number is 4\") {\r\n         THEN(\"The result is 4\") {\r\n            REQUIRE(fizzbuzz(4) == \"4\");\r\n         }\r\n      }\r\n      WHEN(\"The number is 5\") {\r\n         THEN(\"The result is buzz\") {\r\n            REQUIRE(fizzbuzz(5) == \"buzz\");\r\n         }\r\n      }\r\n      WHEN(\"The number is a multiple of 3 only\") {\r\n         THEN(\"The result is fizz\") {\r\n            for (int i = 3; i &lt;= 100; i += 3) {\r\n               if (i % 5) REQUIRE(fizzbuzz(i) == \"fizz\");\r\n            }\r\n         }\r\n      }\r\n      WHEN(\"The number is a multiple of 5 only\") {\r\n         THEN(\"The result is buzz\") {\r\n            for (int i = 5; i &lt;= 100; i += 5) {\r\n               if (i % 3) REQUIRE(fizzbuzz(i) == \"buzz\");\r\n            }\r\n         }\r\n      }\r\n      WHEN(\"The number is a multiple of 3 and 5\") {\r\n         THEN(\"The result is fizzbuzz\") {\r\n            for (int i = 15; i &lt;= 100; i += 15) {\r\n               REQUIRE(fizzbuzz(i) == \"fizzbuzz\");\r\n            }\r\n         }\r\n      }\r\n   }\r\n   GIVEN(\"Any negative number\") {\r\n      WHEN(\"The number is -1\") {\r\n         THEN(\"The result is -1\") {\r\n            REQUIRE(fizzbuzz(-1) == \"-1\");\r\n         }\r\n      }\r\n      WHEN(\"The number is -2\") {\r\n         THEN(\"The result is -2\") {\r\n            REQUIRE(fizzbuzz(-2) == \"-2\");\r\n         }\r\n      }\r\n      WHEN(\"The number is -3\") {\r\n         THEN(\"The result is fizz\") {\r\n            REQUIRE(fizzbuzz(-3) == \"fizz\");\r\n         }\r\n      }\r\n      WHEN(\"The number is -4\") {\r\n         THEN(\"The result is -4\") {\r\n            REQUIRE(fizzbuzz(-4) == \"-4\");\r\n         }\r\n      }\r\n      WHEN(\"The number is -5\") {\r\n         THEN(\"The result is buzz\") {\r\n            REQUIRE(fizzbuzz(-5) == \"buzz\");\r\n         }\r\n      }\r\n   }\r\n}<\/pre>\n<p>This is possible because the macro <tt>SCENARIO<\/tt> resolves to <tt>TEST_CASE<\/tt> and the macros <tt>GIVEN<\/tt>, <tt>WHEN<\/tt>, <tt>AND_WHEN<\/tt>, <tt>THEN<\/tt>, <tt>AND_THEN<\/tt> resolve to <tt>SECTION<\/tt>. Here is how they are defined in the framework:<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\n\/\/ \"BDD-style\" convenience wrappers\r\n#define SCENARIO( ... ) TEST_CASE( \"Scenario: \" __VA_ARGS__ )\r\n#define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, \"Scenario: \" __VA_ARGS__ )\r\n\r\n#define GIVEN( desc )    SECTION( std::string(\"   Given: \") + desc )\r\n#define WHEN( desc )     SECTION( std::string(\"    When: \") + desc )\r\n#define AND_WHEN( desc ) SECTION( std::string(\"And when: \") + desc )\r\n#define THEN( desc )     SECTION( std::string(\"    Then: \") + desc )\r\n#define AND_THEN( desc ) SECTION( std::string(\"     And: \") + desc )\r\n<\/pre>\n<p>Again, if we want to run all tests, we can just execute the app without any filters. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch6.png\" alt=\"\" width=\"553\" height=\"160\" class=\"alignnone size-full wp-image-2788\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch6.png 553w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch6-300x87.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<p>However, you can also specify the name of a test, or a pattern to select that test to be executed, or removed from execution. In the following example we execute the test case called <tt>\"Test positives\"<\/tt>. You can do the same with BDD test cases (i.e. scenarios). However, you need to prefix the name with <tt>\"Scenario: \"<\/tt>, such as in <tt>\"Scenario: BDD test any number\"<\/tt>. This is necessary, because of the way the <tt>SCENARIO<\/tt> macro is defined (i.e. <tt>#define SCENARIO( ... ) TEST_CASE( \"Scenario: \" __VA_ARGS__ )<\/tt>).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch7.png\" alt=\"\" width=\"874\" height=\"282\" class=\"alignnone size-full wp-image-2787\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch7.png 874w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch7-300x97.png 300w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch7-768x248.png 768w\" sizes=\"auto, (max-width: 874px) 100vw, 874px\" \/><\/p>\n<p>You can also list all the tags defined for your test cases. You do that with the <tt>-t<\/tt> option. In this example, we have 3 test cases tagged <tt>[classic]<\/tt> and two tagged <tt>[bdd]<\/tt>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch8.png\" alt=\"\" width=\"563\" height=\"180\" class=\"alignnone size-full wp-image-2786\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch8.png 563w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch8-300x96.png 300w\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" \/><\/p>\n<p>You can use the tag names to execute all the test cases associated with them. In the following example we run all the test cases tagged <tt>[bdd]<\/tt>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch9.png\" alt=\"\" width=\"597\" height=\"155\" class=\"alignnone size-full wp-image-2785\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch9.png 597w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch9-300x78.png 300w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><\/p>\n<p>There are other features that the framework provides, including <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/tostring.md#top\">string conversions<\/a>, <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/logging.md#top\">logging macros<\/a>, <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/event-listeners.md#top\">event listeners<\/a>, <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/reporters.md#top\">reporters<\/tt>, <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/build-systems.md#top\">CI and Build system integration<\/a>.<\/p>\n<p>You can find the project on <a href=\"https:\/\/github.com\/catchorg\/Catch2\">Github<\/a> with <a href=\"https:\/\/github.com\/catchorg\/Catch2\/blob\/master\/docs\/Readme.md#top\">reference documentation<\/a> to help you write tests for your C++11\/14\/17 code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my book, <a href=\"https:\/\/www.packtpub.com\/application-development\/modern-c-programming-cookbook\">Modern C++ Programming Cookbook<\/a>, I discussed several testing frameworks for C++, more precisely, Boost.Test, Google Test, and Catch (which stands for <em>C++ Automated Test Cases in a Header<\/em>). Since the publishing of the book, a new version of Catch, called <a href=\"https:\/\/github.com\/catchorg\/Catch2\">Catch2<\/a> has been released. This provides new functionalities, but also a series of breaking changes with Catch, including the drop of support for pre-C++11 compilers. For a list of changes, you can see the <a href=\"https:\/\/github.com\/catchorg\/Catch2\/releases\/tag\/v2.0.1\">Catch2 release notes<\/a>. Catch2 is available as a single-header library, is open-sources and cross-platform, and written for C++11\/14\/17\/latest. In this article, I want to give a brief example of how you can write tests for C++ using Catch2.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[451,546,547,545,506],"class_list":["post-2772","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-catch","tag-catch2","tag-framework","tag-testing"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"An overview of the Catch2 testing framework for C++.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,testing,framework,catch,catch2\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Writing C++ unit tests with Catch2\" \/>\n\t\t<meta property=\"og:description\" content=\"An overview of the Catch2 testing framework for C++.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2018-03-29T21:46:21+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-06-26T19:53:35+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Writing C++ unit tests with Catch2\" \/>\n\t\t<meta name=\"twitter:description\" content=\"An overview of the Catch2 testing framework for C++.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#article\",\"name\":\"Writing C++ unit tests with Catch2\",\"headline\":\"Writing C++ unit tests with Catch2\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/03\\\/catch1.png\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#articleImage\",\"width\":547,\"height\":163},\"datePublished\":\"2018-03-29T23:46:21+02:00\",\"dateModified\":\"2020-06-26T21:53:35+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":9,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#webpage\"},\"articleSection\":\"C++, C++, catch, catch2, framework, testing\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#listItem\",\"name\":\"Writing C++ unit tests with Catch2\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#listItem\",\"position\":5,\"name\":\"Writing C++ unit tests with Catch2\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/\",\"name\":\"Writing C++ unit tests with Catch2\",\"description\":\"An overview of the Catch2 testing framework for C++.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2018\\\/03\\\/29\\\/writing-cpp-unit-tests-with-catch2\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2018-03-29T23:46:21+02:00\",\"dateModified\":\"2020-06-26T21:53:35+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Writing C++ unit tests with Catch2","description":"An overview of the Catch2 testing framework for C++.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/","robots":"max-image-preview:large","keywords":"c++,testing,framework,catch,catch2","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#article","name":"Writing C++ unit tests with Catch2","headline":"Writing C++ unit tests with Catch2","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2018\/03\/catch1.png","@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#articleImage","width":547,"height":163},"datePublished":"2018-03-29T23:46:21+02:00","dateModified":"2020-06-26T21:53:35+02:00","inLanguage":"en-US","commentCount":9,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#webpage"},"articleSection":"C++, C++, catch, catch2, framework, testing"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#listItem","name":"Writing C++ unit tests with Catch2"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#listItem","position":5,"name":"Writing C++ unit tests with Catch2","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/","name":"Writing C++ unit tests with Catch2","description":"An overview of the Catch2 testing framework for C++.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2018-03-29T23:46:21+02:00","dateModified":"2020-06-26T21:53:35+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Writing C++ unit tests with Catch2","og:description":"An overview of the Catch2 testing framework for C++.","og:url":"https:\/\/mariusbancila.ro\/blog\/2018\/03\/29\/writing-cpp-unit-tests-with-catch2\/","article:published_time":"2018-03-29T21:46:21+00:00","article:modified_time":"2020-06-26T19:53:35+00:00","twitter:card":"summary","twitter:title":"Writing C++ unit tests with Catch2","twitter:description":"An overview of the Catch2 testing framework for C++."},"aioseo_meta_data":{"post_id":"2772","title":"Writing C++ unit tests with Catch2","description":"An overview of the Catch2 testing framework for C++.","keywords":[{"label":"C++","value":"C++"},{"label":"testing","value":"testing"},{"label":"framework","value":"framework"},{"label":"catch","value":"catch"},{"label":"catch2","value":"catch2"}],"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:04:51","updated":"2025-12-12 07:48:31","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":9,"uagb_excerpt":"In my book, Modern C++ Programming Cookbook, I discussed several testing frameworks for C++, more precisely, Boost.Test, Google Test, and Catch (which stands for C++ Automated Test Cases in a Header). Since the publishing of the book, a new version of Catch, called Catch2 has been released. This provides new functionalities, but also a series&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">catch<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">catch2<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">framework<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">testing<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">catch<\/span>","<span class=\"advgb-post-tax-term\">catch2<\/span>","<span class=\"advgb-post-tax-term\">framework<\/span>","<span class=\"advgb-post-tax-term\">testing<\/span>"]}},"comment_count":"9","relative_dates":{"created":"Posted 9 years ago","modified":"Updated 6 years ago"},"absolute_dates":{"created":"Posted on March 29, 2018","modified":"Updated on June 26, 2020"},"absolute_dates_time":{"created":"Posted on March 29, 2018 11:46 pm","modified":"Updated on June 26, 2020 9:53 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-II","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=2772"}],"version-history":[{"count":22,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2772\/revisions"}],"predecessor-version":[{"id":3935,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2772\/revisions\/3935"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=2772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=2772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=2772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}