meto
meto

Reputation: 3719

Test reading from a file using GoogleTest

I am trying to test a document reader using Google Test. My code is organized as follow: there is a Document class which opens the file. A document contains one or more Element, which is a struct with a pointer (field) to a wstring and value. Obviously I would like to open the file just once and then cycle though the various 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?

Upvotes: 4

Views: 10886

Answers (1)

Mark Nunberg
Mark Nunberg

Reputation: 3691

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

In general googletest allows three different modes of resource management:

  • SetUp and TearDown() (run before and after each test function)
  • SetUpTestCase() and TearDownTestCase() (run before and after each class, or "Case")
  • The Environment class which is run before/after the first and last test, respectively.

Upvotes: 3

Related Questions