Unit testing

Post Reply
User avatar
johu
Site Admin
Posts: 5791
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 157 times
Been thanked: 1025 times
Contact:

Unit testing

Post by johu »

I've just committed a few unit tests for the new VCU code (far from complete).

Here's the source: https://github.com/jsphuebner/stm32-sin ... _can2/test

Thought I'd introduce the little "framework" here. Yes I know there's many frameworks but I didn't feel like learning any and it was a good opportunity to brush up my C++ skills.
It consists of the two files "test.h" and "test_main.cpp" and the Makefile.

For adding a series of tests you create a cpp file:

Code: Select all

#include "test.h"

class MyTest: public UnitTest
{
   public:
      MyTest(const std::list<VoidFunction>* cases): UnitTest(cases) {}
      //optional
      void TestSetup();
      void TestCaseSetup();
};

void MyTest::TestSetup()
{
   //Run some initialization once before the test cases
}

void MyTest::TestCaseSetup()
{
   //This code is run before each test case
}

//Test case 1
void TestFu1()
{
   ASSERT(1==1); //run some code and do tests
}

//Test case 2
void TestFu2()
{
}

//Finally, register with the framework
REGISTER_TEST(MyTest, FestFu1, TestFu2);
Of course your test file also has to be added to the Makefile (maybe this can be automated with some wildcard?)

The output then looks like this:

Code: Select all

Starting unit Tests
Test test_fu.cpp::TestBoost1 passed.
Test test_fu.cpp::TestBoost2 passed.
Test test_fu.cpp::TestFU1 passed.
Test test_fu.cpp::TestFU2 passed.
Test test_fu.cpp::TestFU2 passed.
Test test_fu.cpp::TestFU2 passed.
Test test_fu.cpp::TestFU2 passed.
Test test_fu.cpp::TestFUPerc passed.
Test test_fu.cpp::TestFUPerc passed.
Test test_fp.cpp::TestMacros passed.
Test test_fp.cpp::TestMacros passed.
Test test_fp.cpp::TestMacros passed.
Test test_fp.cpp::TestItoa passed.
Test test_fp.cpp::TestItoa passed.
Test test_fp.cpp::TestItoa passed.
Test test_fp.cpp::TestItoa passed.
Test test_fp.cpp::TestAtoi passed.
Test test_fp.cpp::TestAtoi passed.
Test test_fp.cpp::TestMedian3 passed.
Test test_fp.cpp::TestMedian3 passed.
Test test_fp.cpp::TestMedian3 passed.
Test test_fp.cpp::TestMedian3 passed.
Test test_fp.cpp::TestMedian3 passed.
Test test_fp.cpp::TestAtan2 passed.
Test test_fp.cpp::TestAtan2 passed.
Test test_fp.cpp::TestAtan2 passed.
Test test_fp.cpp::TestAtan2 passed.
Test test_fp.cpp::TestAtan2 passed.
Test test_vcu.cpp::CanTest1 passed.
Test test_vcu.cpp::CanTest1 passed.
Test test_vcu.cpp::CanTest2 passed.
Test test_vcu.cpp::CanTest2 passed.
Test test_vcu.cpp::CanTest2 passed.
Test test_vcu.cpp::CanTest2 passed.
Test test_vcu.cpp::CanTest2 passed.
Test test_vcu.cpp::CanTest3 passed.
Test test_vcu.cpp::CanTest3 passed.
Test test_vcu.cpp::CanTest3 passed.
Test test_vcu.cpp::CanTest3 passed.
Test test_vcu.cpp::CanTest3 passed.
Test test_vcu.cpp::TestCanSeqError1 passed.
Test test_vcu.cpp::TestCanSeqError1 passed.
Test test_vcu.cpp::TestCanSeqError2 passed.
Assertion failed: Param::GetInt(Param::regenpreset) == 0 in test_vcu.cpp : 137
Test test_vcu.cpp::TestCanSeqError2 passed.
Test test_vcu.cpp::TestCanSeqError2 passed.
1 assertions failed

Process returned 255 (0xFF)   execution time : 0.014 s
Press ENTER to continue.
You'll find the test_vcu.cpp is full of stubs/mocks. These should be moved to separate file.
Maybe I'll add those files to libopeninv/test
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
Post Reply