{"id":674,"date":"2015-10-27T11:14:59","date_gmt":"2015-10-27T11:14:59","guid":{"rendered":"http:\/\/coolt.ch\/notizen\/?p=674"},"modified":"2015-10-27T11:14:59","modified_gmt":"2015-10-27T11:14:59","slug":"unit-testing-with-catch","status":"publish","type":"post","link":"https:\/\/coolt.ch\/notizen\/unit-testing-with-catch\/","title":{"rendered":"Unit Testing with Catch"},"content":{"rendered":"<p>Es gibt <a href=\"https:\/\/github.com\/philsquared\/Catch\">CATCH<\/a>, das Testing f\u00fcr C++ erm\u00f6glicht. Es gibt einen <a href=\"http:\/\/hiltmon.com\/blog\/2014\/10\/26\/simple-c-plus-plus-testing-with-catch-in-xcode\/\">Blogeintrag<\/a>, der den Einsatz von CATCH auf hohem Niveau beschreibt. Zum Starten ist das <a href=\"https:\/\/github.com\/philsquared\/Catch\/blob\/master\/docs\/tutorial.md\">Tutorial <\/a>sehr gut.<\/p>\n<pre class=\"\">#<span class=\"pl-k\">define<\/span> <span class=\"pl-en\">CATCH_CONFIG_MAIN<\/span>  <span class=\"pl-c\">\/\/ This tells Catch to provide a main() - only do this in one cpp file<\/span>\r\n#<span class=\"pl-k\">include<\/span> <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>catch.hpp<span class=\"pl-pds\">\"<\/span><\/span>\r\n\r\n<span class=\"pl-k\">unsigned<\/span> <span class=\"pl-k\">int<\/span> <span class=\"pl-en\">Factorial<\/span>( <span class=\"pl-k\">unsigned<\/span> <span class=\"pl-k\">int<\/span> number ) {\r\n    <span class=\"pl-k\">return<\/span> number &lt;= <span class=\"pl-c1\">1<\/span> ? number : <span class=\"pl-c1\">Factorial<\/span>(number-<span class=\"pl-c1\">1<\/span>)*number;\r\n}\r\n\r\n<span class=\"pl-en\">TEST_CASE<\/span>( <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>Factorials are computed<span class=\"pl-pds\">\"<\/span><\/span>, <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>[factorial]<span class=\"pl-pds\">\"<\/span><\/span> ) {\r\n    <span class=\"pl-c1\">REQUIRE<\/span>( <span class=\"pl-c1\">Factorial<\/span>(<span class=\"pl-c1\">1<\/span>) == <span class=\"pl-c1\">1<\/span> );\r\n    <span class=\"pl-c1\">REQUIRE<\/span>( <span class=\"pl-c1\">Factorial<\/span>(<span class=\"pl-c1\">2<\/span>) == <span class=\"pl-c1\">2<\/span> );\r\n    <span class=\"pl-c1\">REQUIRE<\/span>( <span class=\"pl-c1\">Factorial<\/span>(<span class=\"pl-c1\">3<\/span>) == <span class=\"pl-c1\">6<\/span> );\r\n    <span class=\"pl-c1\">REQUIRE<\/span>( <span class=\"pl-c1\">Factorial<\/span>(<span class=\"pl-c1\">10<\/span>) == <span class=\"pl-c1\">3628800<\/span> );\r\n}<\/pre>\n<p><strong>Catch innerhalb eines Files einbauen und ausf\u00fchren<\/strong><\/p>\n<p>1. Catch als Header in File einbinden<\/p>\n<pre class=\"lang:sh decode:true\">#define CATCH_CONFIG_RUNNER  \r\n#include \"catch.hpp\"<\/pre>\n<p>2. Funktion, die getetst werden will<\/p>\n<pre class=\"lang:sh decode:true\">double <strong>calculate<\/strong>(string calculation) {\r\n    double result = 0.0;\r\n    ts.set(calculation);\r\n    result = expression();\r\n    return result;\r\n}<\/pre>\n<p>3. Testf\u00e4lle aufstellen<\/p>\n<pre class=\"lang:sh decode:true \">TEST_CASE( \"r\", \"[digit]\" ) {\r\n    REQUIRE(<strong>calculate(\"4\")<\/strong> == 4);\r\n    REQUIRE(calculate(\"6\") == 6);\r\n    REQUIRE(calculate(\"4+2\") == 6);\r\n    REQUIRE(calculate(\"4+2+2\") == 8);\r\n    REQUIRE(calculate(\"4+2+2+4+2+2+1\") == 17);\r\n    REQUIRE(calculate(\"4-2\") == 2);\r\n    REQUIRE(calculate(\"8-2\") == 6);\r\n    REQUIRE(calculate(\"8-2-2\") == 4);\r\n    REQUIRE(calculate(\"3*2\") == 6);\r\n    REQUIRE(calculate(\"3*3\") == 9);\r\n    REQUIRE(calculate(\"3*2*3\") == 18);\r\n    REQUIRE(calculate(\"3+2*4\") == 11);\r\n    REQUIRE(calculate(\"3*2+4\") == 10);\r\n    REQUIRE(calculate(\"4 + 2\") == 6);\r\n    REQUIRE(calculate(\"10 + 2\") == 12);\r\n    REQUIRE(calculate(\"10 + 123\") == 133);\r\n    REQUIRE(calculate(\"-4 + 5\") == 1);\r\n    REQUIRE(calculate(\"-100 + 5\") == -95);\r\n    REQUIRE(calculate(\"(4 + 5)*2\") == 18);\r\n    REQUIRE(calculate(\"3+(-4 * 5)\") == -17);\r\n    REQUIRE(calculate(\"1.5 * 2\") == 3);\r\n}<\/pre>\n<p>Am Ende des Files folgend die Testf\u00e4lle.<\/p>\n<p>4. Datei kompilieren<br \/>\n5. Ausf\u00fchren \u00fcber Konsole und Argumente mitgeben<br \/>\nGibt man keine Argumente mit, werden alle Tests durchgelauften.<\/p>\n<p><strong>Struktur der Testf\u00e4lle<\/strong><\/p>\n<pre class=\"lang:sh decode:true\">REQUIRE(calculate(\"6\") == 6);<\/pre>\n<p>Nach REQUIRE schreibt man die Funktion, die aufgerufen werden soll und gibt ihr die notwendigen Werte mit.<br \/>\nNach dem == folgt das zu erwartende Ergebnis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Es gibt CATCH, das Testing f\u00fcr C++ erm\u00f6glicht. Es gibt einen Blogeintrag, der den Einsatz von CATCH auf hohem Niveau beschreibt. Zum Starten ist das Tutorial sehr gut. #define CATCH_CONFIG_MAIN \/\/ This tells Catch to provide a main() &#8211; only do this in one cpp file #include &#8222;catch.hpp&#8220; unsigned int Factorial( unsigned int number ) &hellip; <a href=\"https:\/\/coolt.ch\/notizen\/unit-testing-with-catch\/\" class=\"more-link\"><span class=\"screen-reader-text\">Unit Testing with Catch<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,60],"tags":[],"_links":{"self":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/674"}],"collection":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":5,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":681,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/674\/revisions\/681"}],"wp:attachment":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}