简体   繁体   English

C ++相当于Python的doctests?

[英]C++ equivalent to Python's doctests?

I think the concept of Python's doctests is brilliant, and as a C++ programmer at a real-time shop, I'm quite jealous. 我认为Python的doctests的概念很棒,作为一个实时商店的C ++程序员,我非常嫉妒。 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? 我已经看过C ++ Unit等,但有没有什么可以从像Python的doctests这样的注释中提取测试用例而不是直接将它们放在代码中?

I just released doctest - The lightest feature rich C++ single header testing framework. 我刚刚发布了doctest - 最轻的功能丰富的C ++单头测试框架。

It isn't for writing tests in comments - but for writing tests directly in the production code. 它不是用于在注释中编写测试 - 而是用于直接在生产代码中编写测试。 It doesn't fit your needs perfectly but it is still the best option in C++ without preprocessing steps 它完全不能满足您的需求,但它仍然是没有预处理步骤的C ++中的最佳选择

You might find this useful. 你可能会觉得这很有用。 Ive started developing this after needing this in my own code. 在我自己的代码中需要这个后,我开始开发它。

http://github.com/panyam/DocTestPlusPlus http://github.com/panyam/DocTestPlusPlus

It is a python script that goes through your comments and extracts tests and generates test files. 它是一个python脚本,通过您的注释,提取测试并生成测试文件。

Still under development and testing. 仍在开发和测试中。 Appreciate any and all feedback. 感谢所有反馈。

cheers Sri 欢呼斯里兰卡

I have just had a look at doctest, it is indeed brilliant. 我刚刚看过doctest,它的确很精彩。 So is the ease of use approach of the Python language. Python语言的易用性方法也是如此。

For C++ however, you probably won't find such a tool. 但是对于C ++,您可能找不到这样的工具。

If one of the tests fails you might want to debug what happens. 如果其中一个测试失败,您可能需要调试发生的情况。 It wouldn't be that easy having the source of the test case been generated from comments. 从评论中生成测试用例的源代码并不容易。 Instead, in the existing unit test frameworks for C++ you have the source of the tests with a special syntax, which is compiled and is easy to debug. 相反,在现有的C ++单元测试框架中,您可以使用特殊语法来测试源代码,该语法经过编译且易于调试。 Also, the generation of the source from comments would be another extra (pre-)compilation step, which would just make life harder. 此外,从注释生成源代码将是另一个额外的(预)编译步骤,这将使生活更加艰难。

You should accept that coding in C++ is not as a quick job as it is in Python, so the unit testing is similarly somewhat more difficult. 您应该接受C ++中的编码并不像Python中那样快速,因此单元测试同样有点困难。 On the other hand you have more tools, you can put static assertions for type relationships, for example, which is impossible in Python. 另一方面,你有更多的工具,你可以为类型关系设置静态断言,例如,这在Python中是不可能的。

So briefly I think the analogue of the Python doctest for C++ would have so many drawbacks compared to the existing tools, that nobody began to implement it. 简而言之,我认为与现有工具相比,Python doctest for C ++的类比会有许多缺点,没有人开始实现它。

If you really believe that it can be better than the existing tools, please provide some examples. 如果您确实认为它可能比现有工具更好,请提供一些示例。 I hardly believe that there are real life cases where it can be more usable. 我几乎不相信有真实的生活情况可以更有用。

The test framework that comes with Fost does handle something fairly similar. Fost附带的测试框架确实处理了类似的东西。 The tests would not be embedded in the documentation, but they can sit alongside the code that they test. 测试不会嵌入到文档中,但它们可以与他们测试的代码并列。 In structure the tests look very similar to the cxxtest code. 在结构中,测试看起来非常类似于cxxtest代码。

#include "myclass.hpp"
#include <fost/test>

FSL_TEST_SUITE( myclass );

/*
    Your documentation
*/
FSL_TEST_FUNCTION( constructors ) {
    fostlib::test::default_constructable< myclass >();
}
myclass::myclass() {
}

FSL_TEST_FUNCTION( some_method ) {
    myclass instance;
    FSL_CHECK_NOTHROW( instance.some_method(0) );
    FSL_CHECK_EQ( instance.some_method(2), 2 );
    FSL_CHECK_NEQ( instance.some_method(-2), 0 );
}
int myclass::some_method( int arg ) {
    // implementation
}

All of this lot gets compiled with the tests embedded (you could remove them from the build via a #define -- not implemented, but easy to do). 所有这些都是通过嵌入测试编译的(你可以通过#define从构建中删除它们 - 没有实现,但很容易做到)。 The tests are then run through a separate program that loads the .DLL or .so that has been built, finds the tests and runs them. 然后,测试将通过一个单独的程序运行,该程序加载已构建的.DLL或.so,查找测试并运行它们。

We've not tried it, but it should work with static libraries and dynamically loading and running tests found in .EXE files on Windows, but I'm less sure if it can be done like that on Linux or Macs. 我们没有尝试过,但是它应该可以与静态库一起使用,并动态加载和运行Windows上的.EXE文件中的测试,但我不太确定它是否可以在Linux或Mac上完成。

I was thinking something along the lines of generating CxxTest files from comments. 我正在考虑从评论中生成CxxTest文件的方法。 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. 我的提议是一个解析器,它从注释中提取那些testX函数的内容,而不是必须编写整个东西。 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. 我不确定如何实现CxxTest更强大的方面,比如创建fixture,但是像这样的东西可能提供了C ++世界中python docstrings和doctests的结合。

I'm aware of old tricks with putting things in real code comments (IIRC, this is part of Practice of Programming ). 我知道将旧内容放入实际代码注释中的旧技巧(IIRC,这是编程实践的一部分)。 However, it may be easier to simply put the unit tests in #ifdef blocks. 但是,将单元测试简单地放在#ifdef块中可能更容易。 You can generally run the preprocessor by itself for this sort of thing. 对于这种事情,您通常可以单独运行预处理器。

Then again, I'm aware of one project that uses Perl as a super-duper preprocessor. 然后,我知道一个使用Perl作为超级预处理器的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM