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