<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.natea.ro/index.php?action=history&amp;feed=atom&amp;title=Test_driven_development</id>
	<title>Test driven development - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.natea.ro/index.php?action=history&amp;feed=atom&amp;title=Test_driven_development"/>
	<link rel="alternate" type="text/html" href="http://wiki.natea.ro/index.php?title=Test_driven_development&amp;action=history"/>
	<updated>2026-11-19T07:58:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.natea.ro/index.php?title=Test_driven_development&amp;diff=35&amp;oldid=prev</id>
		<title>Nicu: Created page with &quot;Principiu de baza e ca inainte sa scriem o linie de cod functional, ar trebui sa definim un test.&lt;br /&gt; Orice modificare adusa codului ar trebui sa fie bazata pe o cerinta pe...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.natea.ro/index.php?title=Test_driven_development&amp;diff=35&amp;oldid=prev"/>
		<updated>2020-08-25T19:10:26Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Principiu de baza e ca inainte sa scriem o linie de cod functional, ar trebui sa definim un test.&amp;lt;br /&amp;gt; Orice modificare adusa codului ar trebui sa fie bazata pe o cerinta pe...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Principiu de baza e ca inainte sa scriem o linie de cod functional, ar trebui sa definim un test.&amp;lt;br /&amp;gt;&lt;br /&gt;
Orice modificare adusa codului ar trebui sa fie bazata pe o cerinta pe care sa o putem descrie mai intai printr-un test.&amp;lt;br /&amp;gt;&lt;br /&gt;
Ruland toate testele anterioare dupa fiecare modificare ne asiguram ca nu introducem regresii, deci ar trebui sa pierdem mult mai putin timp facand debug. &amp;lt;br /&amp;gt;&lt;br /&gt;
Dat fiind ca e greu sa satisfaci cerinta cea mai mare(&amp;quot;scrie aplicatia de facut placinte&amp;quot;), lucrul se sparge in cerinte mai mici si de o complexitate mai redusa(&amp;quot;implementeaza clasa x&amp;quot;/&amp;quot;scrie un algoritm de sortare&amp;quot;/...). Deci, prioritizam taskurile scurte in speranta ca in final nu vor exista taskuri complicate, ci doar mici modificari incrementale.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In linii mari:&lt;br /&gt;
* Adaugam un test&lt;br /&gt;
* Rulam toate testele, iar cel nou ar trebui sa pice.&lt;br /&gt;
* Scriem codul sa treaca &lt;br /&gt;
* Rulam toate testele din nou iar toate ar trebui sa treaca.&lt;br /&gt;
* Curatam codul/testele daca e necesar si verificam ca testele trec in continuare.&lt;br /&gt;
* Repetam&lt;br /&gt;
&lt;br /&gt;
Lucruri de tinut cont.&lt;br /&gt;
* Un test case nu ar trebui sa depinda de starea unui test anterior. Fara dependinte intre teste.&lt;br /&gt;
* Testele nu trebuie sa fie foarte complicate si sa testeze absolut toate scenariile&lt;br /&gt;
* Nu-i neaparat sa testam fiecare functie ajutatoare, dar ideal e sa testam toate interfetele publice&lt;br /&gt;
&lt;br /&gt;
==Google Test==&lt;br /&gt;
Pentru a facilita scriere de teste am putea folosi o librarie de la google numita gtest.&lt;br /&gt;
&lt;br /&gt;
Aceasta presupune crearea unei clase ce mosteneste testing::Test, in care putem implementa un set de functii ce vor fi rulate atunci cand pornim/oprim testarea(SetUpTestCase/TearDownTestCase) dar si cand rulam teste individuale(SetUp/TearDown).&lt;br /&gt;
&lt;br /&gt;
Exemplu:&lt;br /&gt;
 #include &amp;lt;iostream&amp;gt;&lt;br /&gt;
 #include &amp;quot;gtest/gtest.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 namespace test&lt;br /&gt;
 {&lt;br /&gt;
   class Super_test: public testing::Test&lt;br /&gt;
   {&lt;br /&gt;
   protected:&lt;br /&gt;
      static void SetUpTestCase()&lt;br /&gt;
      {&lt;br /&gt;
         std::cout &amp;lt;&amp;lt; &amp;quot;Executam &amp;quot; &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      static void TearDownTestCase()&lt;br /&gt;
      {&lt;br /&gt;
         std::cout &amp;lt;&amp;lt; &amp;quot;Executam &amp;quot; &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      virtual void SetUp()&lt;br /&gt;
      {&lt;br /&gt;
         std::cout &amp;lt;&amp;lt; &amp;quot;Executam &amp;quot; &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      }&lt;br /&gt;
 &lt;br /&gt;
      virtual void TearDown()&lt;br /&gt;
      {&lt;br /&gt;
         std::cout &amp;lt;&amp;lt; &amp;quot;Executam &amp;quot; &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      }&lt;br /&gt;
   };&lt;br /&gt;
 } // namespace test&lt;br /&gt;
&lt;br /&gt;
Presupunand am parcurs cerintele putem scrie primul test folosindu'ne de un set de macrouri pe care le gasim in [https://github.com/google/googletest/blob/master/googletest/include/gtest/gtest.h gtest.h]&lt;br /&gt;
&lt;br /&gt;
Exemplu:&lt;br /&gt;
 #include &amp;quot;functii_speciale.hpp&amp;quot;&lt;br /&gt;
 #include &amp;quot;super_test.hpp&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 namespace test&lt;br /&gt;
 {&lt;br /&gt;
   TEST_F(Super_test, test_to_pass)&lt;br /&gt;
   {&lt;br /&gt;
      std::cout &amp;lt;&amp;lt; &amp;quot;Executam &amp;quot; &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      ASSERT_NO_THROW(functie_goala());&lt;br /&gt;
      EXPECT_EQ(1, 1);&lt;br /&gt;
      EXPECT_TRUE(true);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   TEST_F(Super_test, test_to_fail)&lt;br /&gt;
   {&lt;br /&gt;
      std::cout &amp;lt;&amp;lt; &amp;quot;Executam &amp;quot; &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      EXPECT_FALSE(true) &amp;lt;&amp;lt; &amp;quot;Daca vrem sa logam un mesaj mai descriptiv cand pica ....&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   TEST_F(Super_test, test_to_fail2)&lt;br /&gt;
   {&lt;br /&gt;
      std::cout &amp;lt;&amp;lt; &amp;quot;Executam &amp;quot; &amp;lt;&amp;lt; __PRETTY_FUNCTION__ &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      ASSERT_NO_THROW(functie_ce_arunca());&lt;br /&gt;
   }&lt;br /&gt;
 } // namespace test&lt;br /&gt;
&lt;br /&gt;
TEST_F indica un scenariu de test nou. Putem avea nenumarate scenarii in acelasi fisier.&amp;lt;br /&amp;gt;&lt;br /&gt;
Acesta primeste ca parametru clasa creata anterior si numele testului.&amp;lt;br /&amp;gt;&lt;br /&gt;
In interior descriem scenariul pe care il urmarim. ex. ne asteptam ca &amp;quot;functie_goala()&amp;quot; sa nu arunce o exceptie, sau ca 1==1, etc.&amp;lt;br /&amp;gt;&lt;br /&gt;
De notat: parametrii primiti pt EXPECT_EQ(), ca si conventie primul parametrul e valoarea asteptata, iar cel de'al doilea e valoarea reala.&amp;lt;br /&amp;gt;&lt;br /&gt;
Daca testul pica ne putem folosi de operatorul &amp;quot;&amp;lt;&amp;lt;&amp;quot; pt a afisa informatii aditionale. (ex. test_to_fail). Exemplu testezi ceva intr-o bucla si vrei sa stii iteratia la care a picat&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Dupa ce am scris testul urmeaza sa facem implementarea propriu-zisa. In teorie testul e bun, dat fiind ca functiile folosite nu au existat anterior, programul nu compileaza :) Deci, le implementam si facem testul sa treaca&lt;br /&gt;
&lt;br /&gt;
Exemplu:&lt;br /&gt;
 namespace test&lt;br /&gt;
 {&lt;br /&gt;
   void functie_ce_arunca()&lt;br /&gt;
   {&lt;br /&gt;
      throw 1;&lt;br /&gt;
   }&lt;br /&gt;
   void functie_goala()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Evident, tinta e sa scriem teste care trec si nu teste precum: test_to_fail / test_to_fail2&lt;br /&gt;
&lt;br /&gt;
==Build==&lt;br /&gt;
Ca sa putem compila aplicatia de test, trebuie sa linkam cu 3 librarii: gtest_main gtest  si pthread&lt;br /&gt;
&lt;br /&gt;
Evident se poate compila si fara gtest main cat timp adaugati propria voastra functie main:&lt;br /&gt;
 #include &amp;lt;gtest/gtest.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char **argv) {&lt;br /&gt;
    ::testing::InitGoogleTest(&amp;amp;argc, argv); &lt;br /&gt;
    return RUN_ALL_TESTS();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Exemplu makefile:&lt;br /&gt;
 OBJ_DIR := obj&lt;br /&gt;
 SRC_FILES := $(wildcard *.cpp)&lt;br /&gt;
 OBJ_FILES := $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))&lt;br /&gt;
 &lt;br /&gt;
 CPP      := g++&lt;br /&gt;
 LDFLAGS  := -lgtest_main -lgtest -pthread&lt;br /&gt;
 CPPFLAGS := &lt;br /&gt;
 CXXFLAGS := &lt;br /&gt;
 &lt;br /&gt;
 test: $(OBJ_FILES)&lt;br /&gt;
 	$(CPP) $(LDFLAGS) $(INC) -o $@ $^&lt;br /&gt;
 &lt;br /&gt;
 $(OBJ_DIR)/%.o: %.cpp&lt;br /&gt;
 	mkdir -p $(OBJ_DIR)&lt;br /&gt;
 	$(CPP) $(CPPFLAGS) $(INC) $(CXXFLAGS) -c -o $@ $&amp;lt;&lt;br /&gt;
 	&lt;br /&gt;
 .PHONY: clean_test&lt;br /&gt;
 clean_test:&lt;br /&gt;
 	rm -rf $(OBJ_FILES) test&lt;br /&gt;
&lt;br /&gt;
==Run==&lt;br /&gt;
Putem sa rulam direct executabilul rezultat ceea ce ar duce la rularea tuturor testelor:&lt;br /&gt;
&lt;br /&gt;
./test&lt;br /&gt;
 [==========] Running 3 tests from 1 test case.&lt;br /&gt;
 [----------] Global test environment set-up.&lt;br /&gt;
 [----------] 3 tests from Super_test&lt;br /&gt;
 Executam static void test::Super_test::SetUpTestCase()&lt;br /&gt;
 [ RUN      ] Super_test.test_to_pass&lt;br /&gt;
 Executam virtual void test::Super_test::SetUp()&lt;br /&gt;
 Executam virtual void test::Super_test_test_to_pass_Test::TestBody()&lt;br /&gt;
 Executam virtual void test::Super_test::TearDown()&lt;br /&gt;
 [       OK ] Super_test.test_to_pass (0 ms)&lt;br /&gt;
 [ RUN      ] Super_test.test_to_fail&lt;br /&gt;
 Executam virtual void test::Super_test::SetUp()&lt;br /&gt;
 Executam virtual void test::Super_test_test_to_fail_Test::TestBody()&lt;br /&gt;
 test_component.cpp:26: Failure&lt;br /&gt;
 Value of: true&lt;br /&gt;
   Actual: true&lt;br /&gt;
 Expected: false&lt;br /&gt;
 Daca vrem sa logam un mesaj mai descriptiv cand pica ....&lt;br /&gt;
 Executam virtual void test::Super_test::TearDown()&lt;br /&gt;
 [  FAILED  ] Super_test.test_to_fail (0 ms)&lt;br /&gt;
 [ RUN      ] Super_test.test_to_fail2&lt;br /&gt;
 Executam virtual void test::Super_test::SetUp()&lt;br /&gt;
 Executam virtual void test::Super_test_test_to_fail2_Test::TestBody()&lt;br /&gt;
 test_component.cpp:32: Failure&lt;br /&gt;
 Expected: functie_ce_arunca() doesn't throw an exception.&lt;br /&gt;
   Actual: it throws.&lt;br /&gt;
 Executam virtual void test::Super_test::TearDown()&lt;br /&gt;
 [  FAILED  ] Super_test.test_to_fail2 (0 ms)&lt;br /&gt;
 Executam static void test::Super_test::TearDownTestCase()&lt;br /&gt;
 [----------] 3 tests from Super_test (0 ms total)&lt;br /&gt;
 &lt;br /&gt;
 [----------] Global test environment tear-down&lt;br /&gt;
 [==========] 3 tests from 1 test case ran. (0 ms total)&lt;br /&gt;
 [  PASSED  ] 1 test.&lt;br /&gt;
 [  FAILED  ] 2 tests, listed below:&lt;br /&gt;
 [  FAILED  ] Super_test.test_to_fail&lt;br /&gt;
 [  FAILED  ] Super_test.test_to_fail2&lt;br /&gt;
 &lt;br /&gt;
 2 FAILED TESTS&lt;br /&gt;
&lt;br /&gt;
Sau putem rula teste individuale.&lt;br /&gt;
Exemplu: rulam toate testele care se termina in pass.&lt;br /&gt;
&lt;br /&gt;
./test --gtest_filter=*_pass&lt;br /&gt;
 Note: Google Test filter = *_pass&lt;br /&gt;
 [==========] Running 1 test from 1 test case.&lt;br /&gt;
 [----------] Global test environment set-up.&lt;br /&gt;
 [----------] 1 test from Super_test&lt;br /&gt;
 Executam static void test::Super_test::SetUpTestCase()&lt;br /&gt;
 [ RUN      ] Super_test.test_to_pass&lt;br /&gt;
 Executam virtual void test::Super_test::SetUp()&lt;br /&gt;
 Executam virtual void test::Super_test_test_to_pass_Test::TestBody()&lt;br /&gt;
 Executam virtual void test::Super_test::TearDown()&lt;br /&gt;
 [       OK ] Super_test.test_to_pass (0 ms)&lt;br /&gt;
 Executam static void test::Super_test::TearDownTestCase()&lt;br /&gt;
 [----------] 1 test from Super_test (0 ms total)&lt;br /&gt;
 &lt;br /&gt;
 [----------] Global test environment tear-down&lt;br /&gt;
 [==========] 1 test from 1 test case ran. (1 ms total)&lt;br /&gt;
 [  PASSED  ] 1 test.&lt;br /&gt;
&lt;br /&gt;
Pentru parametrii aditionali ce pot fi dati aplicatiei, folositi parametrul help&amp;lt;br /&amp;gt;&lt;br /&gt;
Exemplu: listarea testelor existente, rularea in bucla, ordinea testelor, etc.&lt;br /&gt;
&lt;br /&gt;
./test --help&lt;br /&gt;
 This program contains tests written using Google Test. You can use the&lt;br /&gt;
 following command line flags to control its behavior:&lt;br /&gt;
 &lt;br /&gt;
 Test Selection:&lt;br /&gt;
  --gtest_list_tests&lt;br /&gt;
      List the names of all tests instead of running them. The name of&lt;br /&gt;
      TEST(Foo, Bar) is &amp;quot;Foo.Bar&amp;quot;.&lt;br /&gt;
  --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]&lt;br /&gt;
      Run only the tests whose name matches one of the positive patterns but&lt;br /&gt;
      none of the negative patterns. '?' matches any single character; '*'&lt;br /&gt;
      matches any substring; ':' separates two patterns.&lt;br /&gt;
  --gtest_also_run_disabled_tests&lt;br /&gt;
      Run all disabled tests too.&lt;br /&gt;
 &lt;br /&gt;
 Test Execution:&lt;br /&gt;
  --gtest_repeat=[COUNT]&lt;br /&gt;
      Run the tests repeatedly; use a negative count to repeat forever.&lt;br /&gt;
  --gtest_shuffle&lt;br /&gt;
      Randomize tests' orders on every iteration.&lt;br /&gt;
  --gtest_random_seed=[NUMBER]&lt;br /&gt;
      Random number seed to use for shuffling test orders (between 1 and&lt;br /&gt;
      99999, or 0 to use a seed based on the current time).&lt;br /&gt;
 &lt;br /&gt;
 Test Output:&lt;br /&gt;
  --gtest_color=(yes|no|auto)&lt;br /&gt;
      Enable/disable colored output. The default is auto.&lt;br /&gt;
  --gtest_print_time=0&lt;br /&gt;
      Don't print the elapsed time of each test.&lt;br /&gt;
  --gtest_output=(json|xml)[:DIRECTORY_PATH/|:FILE_PATH]&lt;br /&gt;
      Generate a JSON or XML report in the given directory or with the given&lt;br /&gt;
      file name. FILE_PATH defaults to test_details.xml.&lt;br /&gt;
  --gtest_stream_result_to=HOST:PORT&lt;br /&gt;
      Stream test results to the given server.&lt;br /&gt;
 &lt;br /&gt;
 Assertion Behavior:&lt;br /&gt;
  --gtest_death_test_style=(fast|threadsafe)&lt;br /&gt;
      Set the default death test style.&lt;br /&gt;
  --gtest_break_on_failure&lt;br /&gt;
      Turn assertion failures into debugger break-points.&lt;br /&gt;
  --gtest_throw_on_failure&lt;br /&gt;
      Turn assertion failures into C++ exceptions for use by an external&lt;br /&gt;
      test framework.&lt;br /&gt;
  --gtest_catch_exceptions=0&lt;br /&gt;
      Do not report exceptions as test failures. Instead, allow them&lt;br /&gt;
      to crash the program or throw a pop-up (on Windows).&lt;br /&gt;
 &lt;br /&gt;
 Except for --gtest_list_tests, you can alternatively set the corresponding&lt;br /&gt;
 environment variable of a flag (all letters in upper-case). For example, to&lt;br /&gt;
 disable colored text output, you can either specify --gtest_color=no or set&lt;br /&gt;
 the GTEST_COLOR environment variable to no.&lt;br /&gt;
 &lt;br /&gt;
 For more information, please read the Google Test documentation at&lt;br /&gt;
 https://github.com/google/googletest/. If you find a bug in Google Test&lt;br /&gt;
 (not one in your own code or tests), please report it to&lt;/div&gt;</summary>
		<author><name>Nicu</name></author>
		
	</entry>
</feed>