Skip to content

Dynamically instantiated test suite + cxxtestgen --no-static-init: broken run-time behaviour #144

@mp-chet

Description

@mp-chet

Overview

I have a dynamically instantiated test suite that contains a createSuite() method.
I call cxxtestgen with --no-static-init.

The resulting code misbehaves during execution as follows:

  1. An instance A of my test-suite object gets created.
  2. Another instance B of my test-suite object gets created.
  3. setUp() is called on B.
  4. The test-case methods are called on A, which is obviously uninitialised.
  5. tearDown() is called on B.
  6. B is destroyed.

Minimal example

Save this as bug.h:

#include <cxxtest/TestSuite.h>
  
class BuggyTestSuite : public CxxTest::TestSuite {

public:

    void setUp() override {
        std::cerr << "Calling setUp()        on " << this << std::endl;
    }

    void tearDown() override {
        std::cerr << "Calling tearDown()     on " << this << std::endl;
    }

    void testBreakage() {
        std::cerr << "Calling testBreakage() on " << this << " <- !!"
                  << std::endl;
    }

    static BuggyTestSuite *createSuite() {
        BuggyTestSuite *suite = new BuggyTestSuite();
        std::cerr << "Created " << suite << std::endl;
        return suite;
    }

    static void destroySuite(BuggyTestSuite *suite) {
        std::cerr << "Destroying " << suite << std::endl;
        delete suite;
    }
};

Steps to reproduce

cxxtestgen --error-printer --no-static-init -o bug.cpp bug.h
c++ -o bug bug.cpp
./bug

Example output

Created 0x557bfb48be70
Running cxxtest tests (1 test)Created 0x557bfb48c2c0
Calling setUp()        on 0x557bfb48c2c0
Calling testBreakage() on 0x557bfb48be70 <- !!
Calling tearDown()     on 0x557bfb48c2c0
.Destroying 0x557bfb48c2c0
OK!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions