diff --git a/pw_unit_test/framework_test.cc b/pw_unit_test/framework_test.cc index 10a0adace..1c0a1ce4d 100644 --- a/pw_unit_test/framework_test.cc +++ b/pw_unit_test/framework_test.cc @@ -173,5 +173,23 @@ class Expectations : public ::testing::Test { TEST_F(Expectations, SetCoolNumber) { cool_number_ = 14159; } +class SetUpAndTearDown : public ::testing::Test { + protected: + SetUpAndTearDown() : value_(0) { EXPECT_EQ(value_, 0); } + + ~SetUpAndTearDown() { EXPECT_EQ(value_, 1); } + + void SetUp() override { value_ = 1337; } + + void TearDown() override { value_ = 1; } + + int value_; +}; + +TEST_F(SetUpAndTearDown, MakeSureItIsSet) { + EXPECT_EQ(value_, 1337); + value_ = 3210; +} + } // namespace } // namespace pw diff --git a/pw_unit_test/public/pw_unit_test/framework.h b/pw_unit_test/public/pw_unit_test/framework.h index 64b34f1c0..6115a00bc 100644 --- a/pw_unit_test/public/pw_unit_test/framework.h +++ b/pw_unit_test/public/pw_unit_test/framework.h @@ -338,12 +338,31 @@ class TestInfo { // the block provided to the TEST macro. class Test { public: - // Runs the unit test. Currently, this simply executes the test body, but it - // could be expanded to perform more bookkeeping operations. - void PigweedTestRun() { PigweedTestBody(); } + Test(const Test&) = delete; + Test& operator=(const Test&) = delete; virtual ~Test() = default; + // Runs the unit test. + void PigweedTestRun() { + SetUp(); + PigweedTestBody(); + TearDown(); + } + + protected: + Test() = default; + + // Called immediately before executing the test body. + // + // Setup and cleanup can typically be done in the test fixture's constructor + // and destructor, but there are cases where SetUp/TearDown must be used + // instead. See the Google Test documentation for more information. + virtual void SetUp() {} + + // Called immediately after executing the test body. + virtual void TearDown() {} + private: friend class internal::Framework; @@ -366,6 +385,7 @@ class Test { : public parent_class { \ private: \ void PigweedTestBody() override; \ + \ static ::pw::unit_test::internal::TestInfo test_info_; \ }; \ \