【发布时间】:2020-12-03 06:18:06
【问题描述】:
我正在尝试将模块添加到基于Godot 的游戏项目中。我希望使用doctest 添加单元测试。为简单起见,我将使用link above 中给出的示例。所以我创建了这个简单的文件结构:
summator/
include/
summator.h
src/
summator.cpp
tests/
doctest.h
summator_tests.cpp
SCsub
config.py
register_types.h
register_types.cpp
SConstruct/SCsub
Godot 需要最后四个文件。这里是SConstruct/SCsub(第二个文件名是Godot需要的,我用一个SConstruct文件单独测试):
#!/usr/bin/env python
# import the environment provided by Godot
Import( 'env' )
# when tested in isolation, replace with following line
# env = Environment()
# add include directory to the search path
env.Append( CPPPATH = [ '#include' ] ) # relative path
# add all cpp files so Scons can build the module
env.add_source_files( env.modules_sources, 'src/*.cpp' )
env.add_source_files( env.modules_sources, '*.cpp' )
# if tests are enabled, build them
if env[ 'tests' ]:
SConscript([ 'tests/SCsub' ])
接下来,summator/tests/ 中的 SConscript 应该构建测试:
#!/usr/bin/env python
Import( 'env' )
tests_env = env.Clone()
# build tests
tests = test_env.Program( 'runTest', Glob( '*.cpp' )
当我尝试运行此程序时,我收到一个错误,即找不到标头“summator.h”:
[ 99%] Compiling ==> modules/summator/tests/sumator_tests.cpp
modules/summator/tests/summator_tests.cpp:3:10: fatal error: 'summator.h' file not found
#include "summator.h"
^~~~~~~~~~~~~
1 error generated.
scons: *** [modules/summator/tests/summator_tests.linuxbsd.tools.64.llvm.o] Error 1
scons: building terminated because of errors.
按照我理解 scons 环境的方式,一旦我添加了路径/文件,所有 SConscript(导入环境)都可以访问它。我究竟做错了什么?这感觉应该是微不足道的,但由于某种原因它不起作用。
为了完整起见,这里是其他源文件:
// summator.h
#ifndef SUMMATOR_H
#define SUMMATOR_H
#include "core/reference.h"
class Summator : public Reference
{
GDCLASS( Summator, Reference );
int count;
protected:
static void _bind_methods();
public:
void add( int p_value );
void reset();
int get_total() const;
Summator();
};
#endif // SUMMATOR_H
// summator.cpp
#include "summator.h"
void Summator::add( int p_value )
{
count += p_value;
}
void Summator::reset()
{
count = 0;
}
int Summator::get_total() const
{
return count;
}
void Summator::_bind_methods()
{
ClassDB::bind_method( D_METHOD( "add", "value" ), &Summator::add );
ClassDB::bind_method( D_METHOD( "reset" ), &Summator::reset );
ClassDB::bind_method( D_METHOD( "get_total" ), &Summator::get_total );
}
Summator::Summator()
{
count = 0;
}
// summator_tests.cpp
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
// #include "thirdparty/doctest/doctest.h"
#include "summator.h"
#include "doctest.h"
TEST_CASE( "testing the summator" )
{
// class under test
Summator* cut = new Summator();
cut->add(10);
CHECK( cut->get_total() == 10 )
cut->add(10);
CHECK( cut->get_total() == 20 )
cut->add(10);
CHECK( cut->get_total() == 30 )
cut->reset();
CHECK( cut->get_total() == 0 )
// clean up
delete cut;
}
【问题讨论】:
-
它是否可以单独工作,但不能在 Godot 构建中工作?
-
感谢您的回复,但我已经找到了解决办法!