pw_unit_test: Add support for SetUp / TearDown

Change-Id: Ied8681fde202adec5e2768504c8f8342d72a9156
This commit is contained in:
Wyatt Hepler 2020-02-11 20:19:26 -08:00
parent bc6332cae2
commit fc80d927cf
2 changed files with 41 additions and 3 deletions

View File

@ -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

View File

@ -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_; \
}; \
\