1, 建立头文件calculator.h
//functions.h
int
add
(
int
a,
int
b)
;
int
myMinus
(
int
a,
int
b)
;
int
multiply
(
int
a,
int
b)
;
int
divide
(
int
a,
int
b)
;
//calculator.cpp
int
add
(
int
a,
int
b)
{
return
a+b;
}
int
myMinus
(
int
a,
int
b)
{
return
a-b;
}
int
multiply
(
int
a,
int
b)
{
return
a*b;
}
int
divide
(
int
a,
int
b)
{
return
a/b;
}
3 建立测试文件calculatorTest.cpp
//calculatorTest.cpp
TEST(AddTest,AddTestCase){
ASSERT_EQ(
2
,add(
1
,
1
));
}
TEST(MinusTest,MinusTestCase){
ASSERT_EQ(
10
,myMinus(
25
,
15
));
}
TEST(MultiplyTest,MutilplyTestCase){
ASSERT_EQ(
12
,multiply(
3
,
4
));
}
TEST(DivideTest,DivideTestCase){
ASSERT_EQ(
2
,divide(
7
,
3
));
}
4 建立总测试文件TestAll.cpp
using
namespace
std
;
int
main
(
int
argc,
char
* argv[])
{
testing::GTEST_FLAG(output) =
"xml:"
;
//若要生成xml结果文件
testing::InitGoogleTest(&argc,argv);
//初始化
RUN_ALL_TESTS();
//跑单元测试
return
0
;
}
在GTestApp目录下新建lib目录,并复制libgtest.a到其中
mkdir
lib
cp /home/jerery/googletest-main/lib/
*.a
./lib
编译
g++ calculator.h calculator.cpp calculatorTest.cpp TestAll.cpp -o
test
-lgtest -lgmock -lpthread -std=c++14
运行测试
./test --gtest_output=xml
root@jerry-
virtual
-machine:/home/jerry/googletest-main/googletest/myworkspace/calculator
[
] Running
4
tests
from
4
test suites.
[
] Global test environment
set
-up.
[
]
1
test
from
AddTest
[
] AddTest.AddTestCase
[
] AddTest.AddTestCase (
0
ms)
[
]
1
test
from
AddTest
(
0
ms total
)
[----------] 1 test
from
MinusTest
[ RUN ] MinusTest.MinusTestCase
[ OK ] MinusTest.
MinusTestCase
(
0
ms
)
[----------] 1 test
from
MinusTest
(
0
ms total
)
[----------] 1 test
from
MultiplyTest
[ RUN ] MultiplyTest.MutilplyTestCase
[ OK ] MultiplyTest.
MutilplyTestCase
(
0
ms
)
[----------] 1 test
from
MultiplyTest
(
0
ms total
)
[----------] 1 test
from
DivideTest
[ RUN ] DivideTest.DivideTestCase
[ OK ] DivideTest.
DivideTestCase
(
0
ms
)
[----------] 1 test
from
DivideTest
(
0
ms total
)
[----------] Global test environment tear-down
[
==========]
4
tests
from
4
test suites ran. (
0
ms total)
[
]
4
tests.
查看产生xml文件:test_detail.xml
cat test_detail.xml
?xml version="1.0" encoding="UTF-8"?>
<
testsuites
tests
=
"4"
failures
=
"0"
disabled
=
"0"
errors
=
"0"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.201"
name
=
"AllTests"
>
<
testsuite
name
=
"AddTest"
tests
=
"1"
failures
=
"0"
disabled
=
"0"
skipped
=
"0"
errors
=
"0"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.201"
>
<
testcase
name
=
"AddTestCase"
file
=
"calculatorTest.cpp"
line
=
"5"
status
=
"run"
result
=
"completed"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.201"
classname
=
"AddTest"
/>
</
testsuite
>
<
testsuite
name
=
"MinusTest"
tests
=
"1"
failures
=
"0"
disabled
=
"0"
skipped
=
"0"
errors
=
"0"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.202"
>
<
testcase
name
=
"MinusTestCase"
file
=
"calculatorTest.cpp"
line
=
"8"
status
=
"run"
result
=
"completed"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.202"
classname
=
"MinusTest"
/>
</
testsuite
>
<
testsuite
name
=
"MultiplyTest"
tests
=
"1"
failures
=
"0"
disabled
=
"0"
skipped
=
"0"
errors
=
"0"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.202"
>
<
testcase
name
=
"MutilplyTestCase"
file
=
"calculatorTest.cpp"
line
=
"11"
status
=
"run"
result
=
"completed"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.202"
classname
=
"MultiplyTest"
/>
</
testsuite
>
<
testsuite
name
=
"DivideTest"
tests
=
"1"
failures
=
"0"
disabled
=
"0"
skipped
=
"0"
errors
=
"0"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.202"
>
<
testcase
name
=
"DivideTestCase"
file
=
"calculatorTest.cpp"
line
=
"14"
status
=
"run"
result
=
"completed"
time
=
"0."
timestamp
=
"2024-06-30T18:51:31.202"
classname
=
"DivideTest"
/>
</
testsuite
>
</
testsuites
>
原文始发于微信公众号(啄木鸟软件测试):一个google Test文件案例
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论