C ++ unit testing setup () and teardown ()
I am currently looking into unit testing with google mock . What is commonly used virtual void SetUp()
, and virtual void TearDown()
in Google layout? An example scenario with codes would be nice. Thanks in advance.
+3
source to share
1 answer
Here you need to code the code you want to do at the beginning and at the end of each test, so as not to repeat it.
For example:
namespace {
class FooTest : public ::testing::Test {
protected:
Foo * pFoo_;
FooTest() {
}
virtual ~FooTest() {
}
virtual void SetUp() {
pFoo_ = new Foo();
}
virtual void TearDown() {
delete pFoo_;
}
};
TEST_F(FooTest, CanDoBar) {
// You can assume that the code in SetUp has been executed
// pFoo_->bar(...)
// The code in TearDown will be run after the end of this test
}
TEST_F(FooTest, CanDoBaz) {
// The code from SetUp will have been executed *again*
// pFoo_->baz(...)
// The code in TearDown will be run *again* after the end of this test
}
} // Namespace
+4
source to share