Monday, October 21, 2013

VS2012 and Google Test - Using TEST, ASSERT, and EXPECT (Video #2)

This video shows the basic steps to writing Tests in the googletest framework. This is a followup video to Getting Started. When running Unit Level Tests you want to be able to quickly find the tests that failed and get to the code and conditions that caused the failure. One of the tricks you can use is to come up with a naming standard and to use them.

Here is a naming standard that I use:

  • Test Filename - Name of the class or file testing followed by _ULT
  • Test Case Name - Name of the class or filename that you are going to test
  • Test Name - Name of the method followed by the condition you are testing.
    • methodName-PositiveCondition
    • methodName-NegativeCondition
Using these naming standards in googletest is easy using their TEST Macros.
TEST(testCaseName, testName) {
...
}
Where testCaseName is the name of the Test Case and testName is the name of the test. Here is an example of these standards in action.

BitCoinTransaction_ULT.cpp

TEST(bitCoinTransactionULT,addMoney-PositiveAmount) {
...
};

TEST(bitCoinTransactionULT,addMoney-NegativeAmount) {
...
};

In this case we added two tests both in the bitCoinTransactionULT Test Case. Both tests are testing the method addMoney. The conditions PostiveAmount and NegativeAmount are being tested. It is ok to be descriptive. It will make it much easier to find out what is going in when you are looking through test results of hundreds if not thousands of tests.

Here is a quick overview of ASSERT and EXPECT.

—ASSERT_*

—If an assert fails it causes a fatal failure and returns from the current function.
—ASSERT_TRUE(condition) - True
—ASSERT_FALSE(condition) - False
—ASSERT_EQ(expected, actual) - Equal
—ASSERT_NE(expected, actual) - Not Equal
—ASSERT_LT(val1,val2) - Less Than,
ASSERT_LE(val1,val2) - Less Than Equal To,
ASSERT_GT(val1,val2) - Greater Than,
ASSERT_GE(val1,val2) - Greater Than Equal To

—EXPECT_*

—If an expect fails it does not cause a fatal failure. It just flags the failure in the logs
—EXPECT_TRUE(condition) - True
—EXPECT_FALSE(condition) - False
—EXPECT_EQ(expected, actual) - Equal
—EXPECT_NE(expected, actual) - Not Equal
—EXPECT_LT(val1,val2) - Less Than,
EXPECT_LE(val1,val2) - Less Than Equal To,
EXPECT_GT(val1,val2) - Greater Than Equal,
EXPECT_GE(val1,val2) - Greater Than Equal To

No comments:

Post a Comment