New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing private members - ability to write test cases in class bodies #76

Closed
r-barnes opened this Issue Jun 24, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@r-barnes
Copy link

r-barnes commented Jun 24, 2017

Description

Some classes have private methods which cannot be conveniently tested through a public interface.

What is the recommended way to test a private method using doctest?

Extra information

  • doctest version: v1.2.1
  • Operating System: Lubuntu 17.04 (Zesty)
  • Compiler+version: G++ 6.3.0
@onqtam

This comment has been minimized.

Copy link
Owner

onqtam commented Jun 26, 2017

This is the only thing I'm able to come up with:

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

class A {
    void private_method() { printf("hello!\n"); }
    
#ifdef DOCTEST_LIBRARY_INCLUDED
public:
    static void test1() {
        A a;
        a.private_method();
    }
#endif
};

DOCTEST_REGISTER_FUNCTION(A::test1, "test name")

// or like this:
// TEST_CASE("test name") { A::test1(); }

The problem is that I need to create a global dummy variable outside of the class to force the execution of code before main() is called so the test is automatically registered. That's why tests cannot be fully embedded inside of classes...

@onqtam onqtam closed this Aug 3, 2017

@onqtam

This comment has been minimized.

Copy link
Owner

onqtam commented Aug 3, 2017

I'm closing this - not sure what else I could suggest

@onqtam onqtam changed the title Private members [question] Private members Aug 3, 2017

@JankoDedic

This comment has been minimized.

Copy link

JankoDedic commented Feb 9, 2019

Could this be solved using C++17 inline variables? A global dummy variable could be defined inside of the class as inline static.

@onqtam

This comment has been minimized.

Copy link
Owner

onqtam commented Feb 9, 2019

@JankoDedic turns out it is possible! there was one other problem but I managed to solve it, so now I'm thinking of what to call the new macro - perhaps TEST_CASE_CLASS ...?

@onqtam onqtam reopened this Feb 9, 2019

@onqtam onqtam changed the title [question] Private members testing private members - ability to write test cases in class bodies Feb 9, 2019

@JankoDedic

This comment has been minimized.

Copy link

JankoDedic commented Feb 9, 2019

That's amazing to hear! I just made it work a few minutes ago as well :). Not sure about the name, but TEST_CASE_CLASS is good with me :)

@onqtam

This comment has been minimized.

Copy link
Owner

onqtam commented Feb 9, 2019

I just pushed the change in the dev branch - here is how to use it (no matter if in a source or a header file):

class my_type {
    int data = 5;

    TEST_CASE_CLASS("private test! no problem") {
        my_type local;
        CHECK(local.data == 2);
    }

public:
    my_type() = default;

    TEST_CASE_CLASS("public test! doesn't matter") {
        my_type local;
        CHECK(local.data == 2);
    }
};

@onqtam onqtam closed this in a4c5261 Feb 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment