Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent to Python's doctests?

Tags:

c++

doctest

I think the concept of Python's doctests is brilliant, and as a C++ programmer at a real-time shop, I'm quite jealous. We basically have no unit test capability, which is a severe hindrance. I've seen C++Unit, etc, but is there anything that can extract test cases out of comments like Python's doctests rather than putting them in the code directly?

like image 584
user59513 Avatar asked Jan 27 '09 20:01

user59513


People also ask

How do I run all Python Doctests?

Right click on a blank space in the python code, and there is a menu option to run all the Doctests found in the file, not just the tests for one function.


2 Answers

You might find this useful. Ive started developing this after needing this in my own code.

http://github.com/panyam/DocTestPlusPlus

It is a python script that goes through your comments and extracts tests and generates test files.

Still under development and testing. Appreciate any and all feedback.

cheers Sri

like image 118
Sri Avatar answered Oct 23 '22 00:10

Sri


I was thinking something along the lines of generating CxxTest files from comments. I haven't used that framework, but it looks promising. From their manual, a unit test file looks something like this:

 // MyTestSuite.h
 #include <cxxtest/TestSuite.h>

 class MyTestSuite : public CxxTest::TestSuite 
 {
 public:
    void testAddition( void )
    {
       TS_ASSERT( 1 + 1 > 1 );
       TS_ASSERT_EQUALS( 1 + 1, 2 );
    }
 };

My proposal would be a parser that extracts the contents of those testX functions from comments, rather than having to write the whole thing. For example (and I'm just making up the comment syntax here, there may be a cleaner way to write it):

// MyRegularCode.cpp

/// Description of the function here
/// Then test case below that gets extracted
/// and turned into CxxTest .h files
/**testAddition
MyClass mc;
mc.MyFunction();
TS_ASSERT( mc.m_value > 1 );
TS_ASSERT_EQUALS( mc.m_value, 3 );
**/
void MyClass::MyFunction()
{
    m_value = 3;
};

I'm not sure how the more powerful aspects of CxxTest would get implemented, such as creating fixtures, but something like this might provide the together-ness of python docstrings and doctests in the C++ world.

like image 23
user59513 Avatar answered Oct 23 '22 01:10

user59513