简体   繁体   English

使用 GoogleTest 测试从文件中读取

[英]Test reading from a file using GoogleTest

I am trying to test a document reader using Google Test.我正在尝试使用 Google Test 测试文档阅读器。 My code is organized as follow: there is a Document class which opens the file.我的代码组织如下:有一个打开文件的Document类。 A document contains one or more Element , which is a struct with a pointer ( field ) to a wstring and value .一个文档包含一个或多个Element ,它是一个带有指向 wstring 和value的指针( field )的struct Obviously I would like to open the file just once and then cycle though the various Elements .显然,我只想打开文件一次,然后循环浏览各种Elements

class DocumentTest : public ::testing::Test {
protected:
  virtual void SetUp() {
  string path = "path/to/file";
  Document doc;
  doc.Open(path);      
}

  Element* el;
  el = doc.Read();
};

TEST_F(DocumentTest, ReadTest) {
    while (file has still elements) {
      wchar_t* expectedField = L"expectingThis";
      wchar_t* readString = el->field;
      EXPECT_EQ(expectedField,readString);
    }

but this is clearly not working.但这显然行不通。 Where should I declare my init code?我应该在哪里声明我的初始化代码?

https://github.com/google/googletest/blob/main/docs/advanced.md https://github.com/google/googletest/blob/main/docs/advanced.md

In general googletest allows three different modes of resource management:一般来说 googletest 允许三种不同的资源管理模式:

  • SetUp and TearDown() (run before and after each test function) SetUpTearDown() (在每个测试函数之前和之后运行)
  • SetUpTestCase() and TearDownTestCase() (run before and after each class, or "Case") SetUpTestCase()TearDownTestCase() (在每个类之前和之后运行,或“案例”)
  • The Environment class which is run before/after the first and last test, respectively.分别在第一次和最后一次测试之前/之后运行的Environment类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM