Created: 2025-05-21 mié 00:40
Ten siempre este libro a mano: The Pragmatic Programmer ya que está lleno de buenos consejos:
``Test Your Software, or Your Users Will. Test ruthlessly. Don’t make your users find bugs for you.''
``Test early. Test Often. Test Automatically. Tests that run with every build are much more effective than test plans that sit on a shelf.''
Sí, y mucho…juzga tu mismo/a:
As of version 3.16.2 (2016-01-06), the SQLite library consists of approximately 122.9 KSLOC of C code. (KSLOC means thousands of "Source Lines Of Code" or, in other words, lines of code excluding blank lines and comments.) By comparison, the project has 745 times as much test code and test scripts - 91596.1 KSLOC.
El propósito del paso de tests es demostrar que en nuestro software existen fallos, no que está libre de ellos:
``Program testing can be used to show the presence of bugs, but never to show their absence!'' – Edsger Dijkstra
bool es_mayor (int x, int y)
.x == 0, y == 0
x == 0, y != 0
x != 0, y == 0
Pero no una certeza completa!
En base a la granularidad de lo que comprueban podemos hablar de:
¡La alarma tiene que sonar cuando debe hacerlo!.
``Use saboteurs to Test Your Testing'' – Pragmatic Programmer, Tip 64
int test (int a, int b) { return a / (a+b); }
En el makefile correspondiente pondríamos una regla como esta:
poo-p1-test.cc : poo-p1-test.h
cxxtestgen --error-printer -o $@ $^
// File: poo-p1-test.h
#include <cxxtest/TestSuite.h>
class PuntoTestSuite : public CxxTest::TestSuite
{
public:
void testConstructores( void ) {
Punto p0; // 1 //
TS_ASSERT_EQUALS ( p0.getX (), 0.0 );
TS_ASSERT_EQUALS ( p0.getY (), 0.0 );
Punto p1 (p0); // 2 //
TS_ASSERT_EQUALS ( p1.getX (), 0.0 );
TS_ASSERT_EQUALS ( p1.getY (), 0.0 );
}
void testAccesores (void) {
Punto p0;
p0.setX (2.3);
TS_ASSERT_EQUALS ( p0.getX (), 2.3 );
TS_ASSERT_EQUALS ( p0.getX (), p0.getLong () );
p0.setY (4.2);
TS_ASSERT_EQUALS ( p0.getY (), 4.2 );
TS_ASSERT_EQUALS ( p0.getY (), p0.getLat () );
}
};
// File: poo-p1-test.cc
/* Generated file, do not edit */
#ifndef CXXTEST_RUNNING
#define CXXTEST_RUNNING
#endif
#define _CXXTEST_HAVE_STD
#include <cxxtest/TestListener.h>
#include <cxxtest/TestTracker.h>
#include <cxxtest/TestRunner.h>
#include <cxxtest/RealDescriptions.h>
#include <cxxtest/ErrorPrinter.h>
int main() { return CxxTest::ErrorPrinter().run(); } // Main program
#include "poo-p1-test.h"
static PuntoTestSuite suite_PuntoTestSuite;
static CxxTest::List Tests_PuntoTestSuite = { 0, 0 };
CxxTest::StaticSuiteDescription suiteDescription_PuntoTestSuite(
"poo-p1-test.h", 33, "PuntoTestSuite",
suite_PuntoTestSuite, Tests_PuntoTestSuite );
...
static class TestDescription_PuntoTestSuite_testConstructores :
public CxxTest::RealTestDescription {
public:
TestDescription_PuntoTestSuite_testConstructores():CxxTest::RealTestDescription(
Tests_PuntoTestSuite, suiteDescription_PuntoTestSuite,
36, "testConstructores" ) {}
void runTest() { suite_PuntoTestSuite.testConstructores(); }
} testDescription_PuntoTestSuite_testConstructores;
static class TestDescription_PuntoTestSuite_testAccesores :
public CxxTest::RealTestDescription {
public:
TestDescription_PuntoTestSuite_testAccesores():CxxTest::RealTestDescription(
Tests_PuntoTestSuite, suiteDescription_PuntoTestSuite,
50, "testAccesores" ) {}
void runTest() { suite_PuntoTestSuite.testAccesores(); }
} testDescription_PuntoTestSuite_testAccesores;
...
#define BOOST_TEST_MODULE poo-p1 test
#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>
#include <sstream>
#include <Punto.h>
#include <Posicion.h>
#include <Aplicacion.h>
using boost::test_tools::output_test_stream;
BOOST_AUTO_TEST_SUITE ( punto_ts ) // Inicio del test suite
struct F {
F() {
BOOST_TEST_MESSAGE( " setup fixture" );
p0 = new Punto;
p1 = new Punto(*p0);
}
~F() {
delete p0; p0 = NULL;
delete p1; p1 = NULL;
BOOST_TEST_MESSAGE( " teardown fixture" );
}
Punto* p0; Punto* p1;
};
BOOST_FIXTURE_TEST_CASE( constructores_fxt, F )
{
// 1 //
BOOST_CHECK_EQUAL ( p0->getX (), 0.0 );
BOOST_CHECK_EQUAL ( p0->getY (), 0.0 );
// 2 //
BOOST_CHECK_EQUAL ( p1->getX (), 0.0 );
BOOST_CHECK_EQUAL ( p1->getY (), 0.0 );
p0->setX (2.3);
BOOST_CHECK_EQUAL ( p0->getX (), 2.3 );
p0->setY (4.2);
BOOST_CHECK_EQUAL ( p0->getY (), 4.2 );
}
BOOST_AUTO_TEST_CASE( constructores )
{
Punto p0; // 1 //
BOOST_CHECK_EQUAL ( p0.getX (), 0.0 );
BOOST_CHECK_EQUAL ( p0.getY (), 0.0 );
Punto p1 (p0); // 2 //
BOOST_CHECK_EQUAL ( p1.getX (), 0.0 );
BOOST_CHECK_EQUAL ( p1.getY (), 0.0 );
p0.setX (2.3);
BOOST_CHECK_EQUAL ( p0.getX (), 2.3 );
p0.setY (4.2);
BOOST_CHECK_EQUAL ( p0.getY (), 4.2 );
}
BOOST_AUTO_TEST_SUITE_END () // Fin del test suite
enable_testing()
en el archivo
CMakeLists.txt del directorio donde vayamos a generar los tests y
aquellos ejecutables que sean un test se le indican a CMake
mediante la orden add_test(...)
.add_executable (...)
.