TDD للتحكم الدقيق. الجزء الأول: الرحلة الأولى

TDD للتحكم الدقيق. الجزء 1: رحلة
TDD الأولى للمتحكم الدقيق. الجزء 2: كيف يتخلص الجواسيس من تبعيات
TDD للمتحكم الدقيق. الجزء الثالث: الجري على الحديد


, , , , , , . . , . ( – ) , .


, . , .


Test-driven development (TDD). TDD ? .


TDD


, TDD Test-last development (TLD). TDD , .



TDD


TDD :


  • TDD , ;
  • TDD : «» ;
  • , ;
  • TDD ;
  • : - , ;
  • .

. , , . . , Android Studio .


( – ) , , . , . . (Integrated Development Environment – IDE). :


embedded, TLD TDD?

TDD ?

.


embedded


:


  • , , . .;
  • , . . .

, IDE. – Hardware Abstraction Level (HAL). IDE . , . ( ).


, TDD :


  1. ?
  2. ?
  3. ?

, TDD. . , , .



  1. ( ) , , .
  2. TDD, .
  3. () STM32F103C8, , , TDD .

GitLab.



embedded- - , . . , . . UART-, – - . , :


  • UART-;
  • UART-;
  • UART-.


STM32F103C8, STM32 , .



-. , -, . ( Programming manual).
- , .



IDE , , . «» C, ++, . -:


  1. IDE – Visual Studio, , . IDE «» C.
  2. CppUTest – , unit- C/C++.

Visual Studio


- Visual Studio, Visual C++ Tests « Windows». (, spies, mocks, stubs . .).


Tests
  1. Properties -> C/C++ -> General -> Additional Include Directories :
    — $(CPP_U_TEST)\include
    — $(SolutionDir)..\Firmware\Project\Include ( )

  2. Properties -> Linker -> Input :
    — $(CPP_U_TEST)\lib\cpputestd.lib
    — $(SolutionDir)Debug\ProductionCodeLib.lib

    $(CPP_U_TEST) – Windows, cpputest (. ).

    Tests.cpp :

#include "CppUTest/CommandLineTestRunner.h"
int main(int argc, char** argv)
{
    return RUN_ALL_TESTS(argc, argv);
}


ProductionCodeLib, – Visual C++. -, «», . . , STM32F103C8.


ProductionCodeLib

, :


  • Properties -> C/C++ -> General -> Additional Include Directories \$(SolutionDir)..\Firmware\Project\Include\

, «Run», , :


OK (0 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 0 ms)


, .


TDD


«» C, . -, «» . Configurator, :


  • UART-;
  • / -;
  • -.

, . ( ) , . 5 . - Configurator.


-


1. read , -.
2. write -.
3. erase .
4. help .
5. .


ASCII.


CppUTest


ConfiguratorTests.cpp Tests, TDD.


CppUTest .


CppUTest:


TEST_GROUP(TestGroupName)
{
    void setup()
    {
    }

    void teardown()
    {
    }
};

TEST(TestGroupName, TestName)
{
}

:


  • TEST_GROUP – , setup() teardown(), ;
  • setup() – , ;
  • teardown() – , ;
  • TEST – , , ;
  • TestGroupName – , ;
  • TestName – .

. , . , Configurator setup() , teardown() . , , . - , NULL. ShouldNotBeNull.


ShouldNotBeNull:


// ConfiguratorTests.cpp
TEST_GROUP(Configurator)
{
    Configurator * configurator = NULL;
    void setup()
    {
        configurator = Configurator_Create();
    }
    void teardown()
    {
        Configurator_Destroy(configurator);
    }
};

TEST(Configurator, ShouldNotBeNull)
{
    CHECK_TRUE(configurator);
}

, , Configurator_Create Configurator_Destroy. . ProductionCodeLib. Configurator.h Configurator.c, -. Configurator.h . Configurator.c , . . . , .


ShouldNotBeNull:


// Configurator.h
typedef struct ConfiguratorStruct Configurator;
Configurator * Configurator_Create(void);

void Configurator_Destroy(Configurator * self);

// Configurator.c
#include "Configurator.h"

typedef struct ConfiguratorStruct
{
    char command[32];
} ConfiguratorStruct;

Configurator * Configurator_Create(void)
{
    return NULL;
}

void Configurator_Destroy(Configurator * self)
{
}

TDD , , (. . Configurator_Create ). failed, . , test-fails.


:


d:\\exampletdd\\tests\\tests\\configuratortests.cpp(27): error: Failure in TEST(Configurator, ShouldNotBeNull)
CHECK_TRUE(configurator) failed
.
Errors (1 failures, 1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 2 ms)

test-passes . Configurator_Create , ShouldNotBeNull. , Configurator_Destroy.


Configurator_Create Configurator_Destroy :


Configurator * Configurator_Create(void)
{
    Configurator * self = (Configurator*)calloc(1, sizeof(ConfiguratorStruct));
    return self;
}

void Configurator_Destroy(Configurator * self)
{
    if (self == NULL)
    {
        return;
    }
    free(self);
    self = NULL;
}

:


.
OK (1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 0 ms)

, test-passes . , , , , . . , «» 32 #define ( enum const define).


(«» ) #define:


// Configurator.h

#define SERIAL_RECEIVE_BUFFER_SIZE 32

// Configurator.c
typedef struct ConfiguratorStruct
{
    char command[SERIAL_RECEIVE_BUFFER_SIZE];
} ConfiguratorStruct;


, :


  1. , .
  2. IDE .
  3. , «Run» ( ), .

TDD -. «» , .




Raccoon Security – «» , , , .


All Articles