In this Blog you will learn how to optimize setting up common environments for your tests using test fixtures in google test.
What is a test Fixture
A Test Fixture allows you to creates setup and teardown method to run before each test is run in your test case. Test Fixtures are easy to create in google test. You just need to create a class that is the same name as your test case and inherit from the testing::Test class.
class BitcoinWallet_ULT : public testing::Test { public: virtual void SetUp() { } virtual void TearDown() { }};
How to use the Test Fixture
Now that you have a test fixture you need to let you tests know how to use the fixture. This is easy as well. Use the TEST_F macro instead of the TEST macro.
- Without Test Fixture
- TEST(BitcoinWallet_ULT,updateLocalBalanceTest)
- With Test Fixture
- TEST_F(BitcoinWallet_ULT,updateLocalBalanceTest)
Only use the TEST_F macro for tests in you test case that you want to use the test fixture. You don't have to use TEST_F if you test does not require setup.
What happens when TEST_F is called
The TEST_F macro will connect your test into the test fixture. This means that the following things will happen each time the TEST_F macro is run.
- Instantiate a fixture class - Calls new on the BitcoinWallet_ULT class
- Call SetUp on the fixture
- Call the test
- Call TearDown on the fixture
- Destroy the fixture - Calls delete on the BitcoinWallet_ULT object created in step 1
What goes in Setup
Anything that is required to run the test (environment) that is common between all of a majority of the tests. Many tests in the same suite will have common variables that they use to run the tests. This typically include Mocks, and setup environment variables. I typically put these variables in the fixture as attributes. Then in the Setup Method I instantiate the objects and assign them to the attributes so they can be used in the tests. Also don't forget to destroy any objects you create in SetUp in the TearDown method.
class BitcoinWallet_ULT : public testing::Test { public: string myAccount; BitcoinWallet* wallet; BitcoinBank_Mock* bankMock; BitcoinCommerce_Mock* commerceMock; BitcoinCurrency_Mock* currencyMock; virtual void SetUp() { ExternalService::theBank = bankMock = new BitcoinBank_Mock(); ExternalService::theCommerce = commerceMock = new BitcoinCommerce_Mock(); ExternalService::theCurrency = currencyMock = new BitcoinCurrency_Mock(); wallet = new BitcoinWallet(); } virtual void TearDown() { delete(wallet); delete(bankMock); delete(commerceMock); delete(currencyMock); }};
Notice in the SetUp that I am setting variables for the ExternalService as well as the attribute to be used in the TEST_F macro. This allows me to change the behavior of the mock in the test itself. Also for each object I create in the SetUp I have a corresponding delete in the TearDown.
Using the TEST_F Macro
Now that the fixture is complete setup I can use it in my test case. All I need to do is change the TEST macro to TEST_F and remove any of the environment setup that the fixture now handles for me.
Before TEST_F
TEST(BitcoinWallet_ULT,updateLocalBalanceTestNoAccount) { BitcoinBank_Mock* bankMock; ExternalService::theBank = bankMock = new BitcoinBank_Mock(); ON_CALL(*bankMock, changeBalance(_,_)) .WillByDefault(Return(-1)); BitcoinWallet* wallet = new BitcoinWallet(); ASSERT_EQ(wallet->updateLocalBalance(),ActionStatus::AccountNotExist); }
With TEST_F
TEST_F(BitcoinWallet_ULT,updateLocalBalanceTestNoAccount) { ON_CALL(*bankMock, changeBalance(_,_)) .WillByDefault(Return(-1)); ASSERT_EQ(wallet->updateLocalBalance(),ActionStatus::AccountNotExist); }
I hope this helps with making your Unit Level Testing more effective.
DWP
No comments:
Post a Comment