如何使用scons运行gtest
How to run gtest using scons
我有一个预编译的gtest版本(我知道Google不建议这样做,但这就是我们项目将使用它的方式),我想编写一个非常简单的测试并使用scons构建它。假设我有一个超级简单的测试,名为test.cpp, gtest安装在/opt/gtest中。我不太擅长使用Scons,我想知道我的SConstruct应该是什么样子的。
//test.cpp
#include "gtest/gtest.h"
TEST(MyTest, Test) {
ASSERT_TRUE(true);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我当前的SConstruct看起来像这样(但不工作)
env = Environment()
LIBS =''
common_libs = ['pthread', 'gtest']
env.Append( LIBS = common_libs )
Program('test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
当我运行scons
时,我得到以下消息scons: Reading SConscript files ...
IndexError: list index out of range:
File "/home/user/testing/SConstruct", line 8:
Program('test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
File "/usr/lib/scons/SCons/Script/SConscript.py", line 614:
return method(*args, **kw)
File "/usr/lib/scons/SCons/Environment.py", line 258:
return MethodWrapper.__call__(self, target, source, *args, **kw)
File "/usr/lib/scons/SCons/Environment.py", line 222:
return self.method(*nargs, **kwargs)
File "/usr/lib/scons/SCons/Builder.py", line 632:
return self._execute(env, target, source, OverrideWarner(kw), ekw)
File "/usr/lib/scons/SCons/Builder.py", line 540:
source = self.src_builder_sources(env, source, overwarn)
File "/usr/lib/scons/SCons/Builder.py", line 736:
s = self._adjustixes(s, None, src_suf)[0]
谢谢!
编辑:
修改
后Program('test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
Program('test', 'test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
我得到以下错误
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o test test.o -L/opt/gtest/lib
test.o: In function `MyTest_Test_Test::TestBody()':
test.cpp:(.text+0x5f): undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)'
test.cpp:(.text+0x8c): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
test.cpp:(.text+0x9f): undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
test.cpp:(.text+0xb2): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
test.cpp:(.text+0xc6): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
test.o: In function `main':
test.cpp:(.text+0x16a): undefined reference to `testing::InitGoogleTest(int*, char**)'
test.cpp:(.text+0x16f): undefined reference to `testing::UnitTest::GetInstance()'
test.cpp:(.text+0x177): undefined reference to `testing::UnitTest::Run()'
test.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x1df): undefined reference to `testing::internal::GetTestTypeId()'
test.cpp:(.text+0x20e): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
test.o: In function `MyTest_Test_Test::MyTest_Test_Test()':
test.cpp:(.text._ZN16MyTest_Test_TestC2Ev[_ZN16MyTest_Test_TestC5Ev]+0x14): undefined reference to `testing::Test::Test()'
test.o: In function `testing::internal::scoped_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
test.cpp:(.text._ZN7testing8internal10scoped_ptrISsE5resetEPSs[testing::internal::scoped_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)]+0x24): undefined reference to `testing::internal::IsTrue(bool)'
test.o: In function `testing::internal::scoped_ptr<std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)':
test.cpp:(.text._ZN7testing8internal10scoped_ptrISt18basic_stringstreamIcSt11char_traitsIcESaIcEEE5resetEPS6_[testing::internal::scoped_ptr<std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)]+0x23): undefined reference to `testing::internal::IsTrue(bool)'
test.o:(.rodata._ZTV16MyTest_Test_Test[vtable for MyTest_Test_Test]+0x20): undefined reference to `testing::Test::SetUp()'
test.o:(.rodata._ZTV16MyTest_Test_Test[vtable for MyTest_Test_Test]+0x28): undefined reference to `testing::Test::TearDown()'
test.o:(.rodata._ZTI16MyTest_Test_Test[typeinfo for MyTest_Test_Test]+0x10): undefined reference to `typeinfo for testing::Test'
test.o: In function `MyTest_Test_Test::~MyTest_Test_Test()':
test.cpp:(.text._ZN16MyTest_Test_TestD2Ev[_ZN16MyTest_Test_TestD5Ev]+0x1f): undefined reference to `testing::Test::~Test()'
collect2: ld returned 1 exit status
scons: *** [test] Error 1
scons: building terminated because of errors.
请换行:
Program('test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
Program('test', 'test.cpp', LIBS, LIBPATH='/opt/gtest/lib')
您将得到一个名为test
的二进制文件。
示例代码
假设我有这样的c++代码:
#include "sample1.h"
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
头文件simple1.h
是:
#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n);
#endif // GTEST_SAMPLES_SAMPLE1_H_
测试代码测试代码如下:
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
TEST(FactorialTest, Negative) {
// This test is named "Negative", and belongs to the "FactorialTest"
// test case.
EXPECT_EQ(1, Factorial(0));
EXPECT_EQ(2, Factorial(2));
EXPECT_GT(Factorial(-10), 0);
}
Google测试库和头文件
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
TEST(FactorialTest, Negative) {
// This test is named "Negative", and belongs to the "FactorialTest"
// test case.
EXPECT_EQ(1, Factorial(0));
EXPECT_EQ(2, Factorial(2));
EXPECT_GT(Factorial(-10), 0);
}
我复制了/usr/gtest/include
中所有的头文件,我也复制了/usr/gtest
目录下的libgtest_main.a
和libgtest.a
两个库文件。我不需要测试主文件,因为我将使用libgtest_main.a
。
sons配置文件。
这是文件:
env = Environment(CPPPATH=['/usr/gtest/include'])
common_libs = ['gtest_main', 'gtest']
env.Append( LIBS = common_libs )
env.Program(source=['sample1.cpp', 'test_sample1.cpp'], LIBPATH=['/usr/gtest'])
运行sons scons -Q
得到:
g++ -o sample1.o -c -I/Users/smcho/Dropbox/smcho/bin/lib/gtest/include sample1.cpp
g++ -o test_sample1.o -c -I/Users/smcho/Dropbox/smcho/bin/lib/gtest/include test_sample1.cpp
g++ -o sample1 sample1.o test_sample1.o -L/Users/smcho/Dropbox/smcho/bin/lib/gtest -lgtest_main -lgtest
运行sample1
获取:
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from FactorialTest
[ RUN ] FactorialTest.Negative
[ OK ] FactorialTest.Negative (0 ms)
[----------] 1 test from FactorialTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
在做了bikram990建议的修正后。还要确保更改库的顺序。gtest库应该放在第一位;看看这个评论!从一个相关的帖子。
这条线:
common_libs = ['pthread', 'gtest']
common_libs = ['gtest', 'pthread']
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 如何运行 gtest?
- GTest - 在不同的参数上运行多个测试用例
- gtest:在 2 TEST_Fs内运行TEST_P
- 在 gtest 中运行线程
- 使用命令行运行 gtest 会出现错误
- 运行gtest util会失败
- 如何在Google测试(gtest)中使用fixture成员值运行参数化测试
- 如何运行GTEST并确保Sigabrt永远不会发生
- 运行GTest时指针无效
- gtest 设置 cmake 以使其运行
- 使用 Visual Studio 2010 运行 gtest:LNK4098 和 LNK2005
- 使用gtest框架在运行时以编程方式重复确定的测试子集
- 如何使用scons运行gtest
- 如何运行gtest没有从谷歌日志记录