GTEST单元测试框架的使用

单元测试在产品开发中,是对我们代码运行结果核对的有力保障,可以有效减少产品开发测试出现的bug,也能随时记录接口调用示例,并随时对我们的测试目标进行检查。

GTEST测试框架是Goole发布的支持Windows/Linux/MacOS的单元测试框架。

1.TEST宏

TEST(test_case_name, test_name)  -> 创建一个简单的测试案例

TEST_F(test_fixture, test_name)  -> 使用测试夹具进行资源类固定化,可将资源用于多个测试案例中

PS : test_fixture是一个test fixture class,是继承testing::Test的一个类,按照官方示例如下

   class FooTest : public testing::Test {
    protected:
     virtual void SetUp() { b_.AddElement(3); }

     Foo a_;
     Foo b_;
   };

   TEST_F(FooTest, InitializesCorrectly) {
     EXPECT_TRUE(a_.StatusIsOK());
  }

  TEST_F(FooTest, ReturnsElementCountCorrectly) {
    EXPECT_EQ(a_.size(), 0);
     EXPECT_EQ(b_.size(), 1);
   }

下面简单写下测试案例

 1 // 资源固定化
 2 class ReadDicom :public::testing::Test
 3 {
 4 protected:
 5     virtual void SetUp()
 6     {
 7         ct_infilepath = gettestfile("ct.dcm");
 8         ct_outfilepath = gettestfile("ct1.dcm");
 9 
10         st_infilepath = gettestfile("st.dcm");
11         st_outfilepath = gettestfile("st1.dcm");
12 
13         dose_infilepath = gettestfile("dose.dcm");
14         dose_outfilepath = gettestfile("dose1.dcm");
15 
16         dose_infilepath = gettestfile("RD_A 2_6678_20190806175756.dcm");
17         dose_outfilepath = gettestfile("RD_A 2_6678_201908061757561.dcm");
18 
19         plan_infilepath = gettestfile("plan.dcm");
20         plan_outfilepath = gettestfile("plan1.dcm");
21     }
22     DICOMMode dicom;
23     string ct_infilepath;
24     string ct_outfilepath;
25     string st_infilepath;
26     string st_outfilepath;
27     string dose_infilepath;
28     string dose_outfilepath;
29     string plan_infilepath;
30     string plan_outfilepath;
31 private:
32     string gettestfile(string filename)
33     {
34         char buffer[_MAX_PATH];
35         _getcwd(buffer, _MAX_PATH);
36         string filepath = string(buffer) + "\\testfile\\" + filename;
37         return filepath;
38     }
39 };
40 
41 // CT文件读写
42 TEST_F(ReadDicom, CT)
43 {
44     DICOMMode dicom1;
45     ASSERT_TRUE(dicom.ReadDicomInfo(ct_infilepath)) << dicom.get_lasterror();
46     ASSERT_TRUE(dicom1.putDicomType(DicomType::CTImage_Type)) << dicom1.get_lasterror();
47     CT_Dicom t = dicom.getCTDicom();
48     ASSERT_TRUE(dicom1.putCTDicom(t)) << dicom1.get_lasterror();
49     ASSERT_TRUE(dicom1.WriteDicomInfo(ct_outfilepath)) << dicom1.get_lasterror();
50 }
51 
52 // Struct文件读写
53 TEST_F(ReadDicom, STRUCT)
54 {
55     DICOMMode dicom1;
56     ASSERT_TRUE(dicom.ReadDicomInfo(st_infilepath)) << dicom.get_lasterror();
57     ASSERT_TRUE(dicom1.putDicomType(DicomType::RTStruct_Type)) << dicom1.get_lasterror();
58     RTStruct_Dicom t = dicom.getRTStructDicom();
59     ASSERT_TRUE(dicom1.putRTStructDicom(t)) << dicom1.get_lasterror();
60     ASSERT_TRUE(dicom1.WriteDicomInfo(st_outfilepath)) << dicom1.get_lasterror();
61 }
 1 //整套数据读写
 2 TEST(WHOLEFILE, wholefile)
 3 {
 4     string dirpath = "D:\\myfile\\dicom_code\\dicom_file\\Dicom\\DICOMModeUnitTest\\testfile\\Plan";
 5     string outdirpath = "D:\\myfile\\dicom_code\\dicom_file\\Dicom\\DICOMModeUnitTest\\testfile\\Plan1";
 6   
 7     string tmpstr = dirpath + "\\*.*";
 8     HANDLE hFind;
 9     WIN32_FIND_DATA findData;
10     hFind = FindFirstFile(tmpstr.data(), &findData);
11     if (hFind != INVALID_HANDLE_VALUE)
12     {
13         do
14         {
15             if (strcmp(findData.cFileName, ".") == 0 
16                 || strcmp(findData.cFileName, "..") == 0 
17                 || strcmp(findData.cFileName, "Plan") == 0)
18                 continue;
19             string filepath = dirpath + "\\" + findData.cFileName;
20             string outfilepath = outdirpath + "\\" + findData.cFileName;
21             DICOMMode dicom;
22             dicom.ReadDicomInfo(filepath);
23             dicom.WriteDicomInfo(outfilepath);
24             
25         } while (FindNextFile(hFind, &findData));
26     }
27 }

2.ASSERT_、EXPECT_系列的检查结果是否正确

ASSERT_系列:

bool值检查

1、 ASSERT_TRUE(参数),期待结果是true

2、ASSERT_FALSE(参数),期待结果是false

数值型数据检查

3、ASSERT_EQ(参数1,参数2),传入的是需要比较的两个数 equal

4、ASSERT_NE(参数1,参数2),not equal,不等于才返回true

5、ASSERT_LT(参数1,参数2),less than,小于才返回true

6、ASSERT_GT(参数1,参数2),greater than,大于才返回true

7、ASSERT_LE(参数1,参数2),less equal,小于等于才返回true

8、ASSERT_GE(参数1,参数2),greater equal,大于等于才返回true

字符串检查

9、ASSERT_STREQ(expected_str, actual_str),两个C风格的字符串相等才正确返回

10、ASSERT_STRNE(str1, str2),两个C风格的字符串不相等时才正确返回

11、ASSERT_STRCASEEQ(expected_str, actual_str)

12、ASSERT_STRCASENE(str1, str2)

EXPECT_系列和ASSERT_系列用法差不多

在上面某些案例中可以看到<<,这个操作符是可以帮助在出现错误的时候输出某些错误信息到测试界面上,方便我们定位!

 

单元测试YYDS!

后面想到其他的再继续进行补充~

posted @ 2021-08-04 16:47  肠粉君  阅读(813)  评论(0)    收藏  举报