CPPUnit, how do I write a test?

Ok, I basically want the ball to roll and record CPPUnit tests, but I have no idea how to do this. Here I have a code that basically gets a pointer to a Menu button for related button groups and position arguments, how can I start creating a test for this?

CMenuButton* CMenuContainer::GetButton(const enumButtonGroup argGroup, const int32_t argPosition) { CMenuButton* pButton = NULL; if (argGroup < MAX_GROUP_BUTTONS) { pButton = m_ButtonGroupList[argGroup].GetButton(argPosition); } return pButton; 

In response to @Fabio Ceconello, is it possible to install some tests for some code like this?

 unsigned long CCRC32::Reflect(unsigned long ulReflect, const char cChar) { unsigned long ulValue = 0; // Swap bit 0 for bit 7, bit 1 For bit 6, etc.... for(int iPos = 1; iPos < (cChar + 1); iPos++) { if(ulReflect & 1) { ulValue |= (1 << (cChar - iPos)); } ulReflect >>= 1; } return ulValue; } 
+4
source share
1 answer

CppUnit is not suitable for creating automated tests for the user interface. This is more for processing-only units. For example, suppose you created a replacement for std::vector and want to make sure that it behaves like the original, you could write tests that add elements to both your standard and standard implementation, and then do some additional processing (deleting, changing elements, etc.), and after each step, check whether they have a consistent result.

For the user interface, I am not aware of the good open source tools and free tools, but one good commercial tool is Test Bearplet from Smart Bear , among others.

In the second example that you specified, you first need to define a validation for the Reflect () method. For example, you can calculate the result of some values ​​manually to check if the return value for each of them is expected. Or you can use the inverse function, which is known to work completely.

Assuming the first option, you can write a test as follows:

 class CRC32Test : public CppUnit::TestCase { public: CRC32Test( std::string name ) : CppUnit::TestCase( name ) {} void runTest() { struct Sample {unsigned long ulReflect; char cChar; unsigned long result}; static Sample samples[] = { // Put here the pre-calculated values }; int count = sizeof(samples) / sizeof(Sample); for (int i = 0; i < count; ++i) CPPUNIT_ASSERT(subject.Reflect(samples[i].ulReflect, samples[i].cChar) == samples[i].result); } private: CRC32 subject; }; int main(void) { CppUnit::TextUi::TestRunner runner; runner.addTest(new CppUnit::TestCaller<CRC32Test>("test CRC32", &CRC32::runTest)); runner.run(); } 
+4
source

Source: https://habr.com/ru/post/1381730/


All Articles