Blog Home

Don't Put Logic in Tests

2014-07-31

Tests should be simple by stating I/O directly rather than computing them.

Bad

TEST(NavigatorTest, ShouldNavigateToPhotosPage) {
  const std::string baseUrl = "http://plus.google.com/";
  Navigator nav(baseUrl);
  nav.GoToPhotosPage();
  EXPECT_EQ(baseUrl + "/u/0/photos", nav.GetCurrentUrl());
}

Even a simple string concatenation can lead to bugs.

Good

TEST(NavigatorTest, ShouldNavigateToPhotosPage) {
  Navigator nav("http://plug.google.com/");
  nav.GoToPhotosPage();
  EXPECT_EQ("http://plus.google.com//u/0/photos", nav.GetCurrentUrl());
}

If a test requires logic, move that logic out of the test body into utilities and helper functions and write tests for them too.