开发过程中的SDK接口,做单元测试,可以选用GTest,下面给出了简单的演示代码:
class ArticleTest : public ::testing::Test {
protected:
// You can remove any or all of the following functions if their bodies would
// be empty.
ArticleTest() {
// You can do set-up work for each test here.
}
~ArticleTest() override {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
void SetUp() override {
// Code here will be called immediately after the constructor (right
// before each test).
//m_fpdDoc = FPDDocOpen(m_cwTestFIlePath, "");
}
void TearDown() override {
// Code here will be called immediately after each test (right
// before the destructor).
//if (m_fpdDoc != nullptr)
// FPDDocClose(m_fpdDoc);
}
// Class members declared here can be used by all tests in the test suite
// for Foo.
//测试数据
FPD_Document m_fpdDoc;
const FS_LPCWSTR m_cwTestFIlePath = L"D:/File/code/cpp/DocProcess/test/testArticle.pdf";
const FS_LPCSTR m_cTestFIlePath = "D:/File/code/cpp/DocProcess/test/testArticle.pdf";
const FS_LPCWSTR m_cwTestFIleOutPutPath = L"D:/File/code/cpp/DocProcess/test/testOut.pdf";
const FS_LPCSTR m_cTestFIleOutPutPath = "D:/File/code/cpp/DocProcess/test/testOut.pdf";
};
TEST_F(ArticleTest, Common) {
ASSERT_EQ(1, 1);
ASSERT_EQ(1, 2);
}
int StartAllTest(int argc, char **argv, const char* resultPath) {
std::string path = "json:" + std::string(resultPath);
::testing::GTEST_FLAG(output) = path; //加上这条则测试结果以xml格式保存下来。保存路径可以修改,如"xml:testResult.xml表示保存到当前路径该名称
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
测试的结果如下:
{
"tests": 1,
"failures": 1,
"disabled": 0,
"errors": 0,
"timestamp": "2021-06-09T18:09:52Z",
"time": "0.002s",
"name": "AllTests",
"testsuites": [
{
"name": "ArticleTest",
"tests": 1,
"failures": 1,
"disabled": 0,
"errors": 0,
"time": "0.001s",
"testsuite": [
{
"name": "Common",
"status": "RUN",
"time": "0.001s",
"classname": "ArticleTest",
"failures": [
{
"failure": "h:\\git_linkandplugin\\bookmark\\test\\bookmarktest\\bookmarktest.cpp:62\nExpected equality of these values:\n 1\n 2",
"type": ""
}
]
}
]
}
]
}
从上面可以看到一个预期的错误.