GTest的简单实用

开发过程中的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": ""
            }
          ]
        }
      ]
    }
  ]
}

从上面可以看到一个预期的错误.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值