Menu

MwlodarskiHeaderCppTest / Blog: Recent posts

Back to work

Coming back after illness. What is biggest pain? I do not remember what I was doing. No principles. No details.

From [r22] to [r23]

Test Method "template"

I read on my blog that test method should not return value. Seems easy?
In file mwtest/PtrTestMethod.h, I replace the returned value

typedef void(TestCase::*PtrTestMethod)(void);

What should happen?
The entire program should not compile. And so it is.... read more

Posted by Marek Włodarski 2015-02-02

What should the test method return?

Until now

Each test method returns a integer value: 0 means success, 1 means failure.

trivial

 return 0;

no-trivial simulates assertTrue()

    return !(c == 4);

but only once per class

Now

The tests should have many asserts.
There is no sense in returning the value.
So the signature of the test method can be changed, so it returns void

But the result of test method must be evaluated.
Vector of AssertInfo will be used.... read more

Posted by Marek Włodarski 2015-01-21

TestCase file organization

Directory and file structure

Revision [r22]

TestCase/
{...}Test.h - test classes
example.cpp - running test classes
doc/
mwtest/
PtrTestMethod.h - pointer to the test method of TestCase class
TestMethodResult.h - enum with possible results of the test method
AssertInfo.h - structure with the name and result of the assert
MethodContainer.h - structure contains pointer to the method and also its result
Assertions.h - base class defines assertions and pointer to current MethodContainer
TestCase.h - base class for tests... read more

Posted by Marek Włodarski 2015-01-21

The name of the function

The test is a class. Classes have members. So the test class can contain its name.

The test method is a function. There is problem in storing its name internally.

Revision [r15]

I decide to store function name outside function.
There is a struct named MethodContainer that contains method pointer and its properties.

Container for test method

~~~~~~~~~~~~
class NoAssertTestFuncNameTestCase;

typedef int(NoAssertTestFuncNameTestCase::*PtrTestMethod)(void);... read more

Posted by Marek Włodarski 2015-01-19

Obligatory name ot the class

Setting the name is moved to the base class which is the class that declares thet member

I move setting the name property of the test class to the base class

~~~~~~~~~~~~~~~~~~

include <iostream></iostream>

include <string></string>

include <vector></vector>

using namespace std;

class NoAssertTestNameTestCase;

typedef int(NoAssertTestNameTestCase::*PtrTestMethod)(void);

class NoAssertTestNameTestCase {
protected:
int nTests;
int nFails;
vector<ptrtestmethod> tests;
string name; ... read more</ptrtestmethod>

Posted by Marek Włodarski 2015-01-19

Name of the test class

TASK

I want to identify the test class by its name.
I want to display the name of the class in the runTests() methods.
In future the test name will be displayed in the result summary.

Solution in [r11]

Test creator has two possibilities:

  • he could pass the test class name as constructor argument
  • he could omit the constructor arguments

What happens in the second situation?
Constructor sets the class name as the name property.
How to achieve it without reflection?
The __func__contains the name of the current function.
ACTION. The name of the constructor is the same as the class name.
What is the problem?
I must use the name of derived class, not from the derived class.... read more

Posted by Marek Włodarski 2015-01-18

Base class for tests - NoAssertTestCase

Still revision [r10]

Code is similar to previous one.

~~~~~~~~

ifndef NOASSERTTESTCASE_H

define NOASSERTTESTCASE_H

include <iostream></iostream>

include <string></string>

include <vector></vector>

using namespace std;

class NoAssertTestCase;

typedef int(NoAssertTestCase::*PtrTestMethod)(void);

class NoAssertTestCase {
protected:
int nTests;
int nFails;
vector<ptrtestmethod> tests; </ptrtestmethod>

public:
NoAssertTestCase() : nTests(0), nFails(0) { }

void appendTestMethod(PtrTestMethod ptm) {
tests.push_back(ptm);
nTests++;
}

void runTests() {
vector<ptrtestmethod>::const_iterator it;
for(it = tests.begin(); it != tests.end(); ++it) {
int testResult = (this->**it)();
if(testResult) {
nFails++;
}
}
}</ptrtestmethod>

void displayResults() {
cout << nFails << "/" << nTests << " fails" << endl;
}
};

endif // NOASSERTTESTCASE_H

~~~~~~~~... read more

Posted by Marek Włodarski 2015-01-15

SimpleTestCase No inheritance testing

Code

SimpleTestCase.h according to [r10] commit.
Below is the class without specific test.
This "framework" should be moved to base class.

~~~~~~~~~~

ifndef SIMPLETESTCASE_H

define SIMPLETESTCASE_H

include <iostream></iostream>

include <string></string>

include <vector></vector>

using namespace std;

// forward declaration allows defining PtrTestMethod
class SimpleTestCase;

/** Pointer to a test method which is a member of SimpleTestCase

  • NOTE: I has to return int and has to have 0 (zero) args /
    typedef int(SimpleTestCase::
    PtrTestMethod)(void);... read more
Posted by Marek Włodarski 2015-01-15

Getting started

Subversion

Starting svn/sf not sure about importing, commiting, checking out.
I'm creating manually subfolders: trunk, branches, tags.

TestCase in header

The general idea: test runner should be in headers.
So I use only inline functions.

First Approach

No inheritance.

Posted by Marek Włodarski 2015-01-15
MongoDB Logo MongoDB