michael@0: // Copyright 2005, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: // This sample teaches how to reuse a test fixture in multiple test michael@0: // cases by deriving sub-fixtures from it. michael@0: // michael@0: // When you define a test fixture, you specify the name of the test michael@0: // case that will use this fixture. Therefore, a test fixture can michael@0: // be used by only one test case. michael@0: // michael@0: // Sometimes, more than one test cases may want to use the same or michael@0: // slightly different test fixtures. For example, you may want to michael@0: // make sure that all tests for a GUI library don't leak important michael@0: // system resources like fonts and brushes. In Google Test, you do michael@0: // this by putting the shared logic in a super (as in "super class") michael@0: // test fixture, and then have each test case use a fixture derived michael@0: // from this super fixture. michael@0: michael@0: #include michael@0: #include michael@0: #include "sample3-inl.h" michael@0: #include "gtest/gtest.h" michael@0: #include "sample1.h" michael@0: michael@0: // In this sample, we want to ensure that every test finishes within michael@0: // ~5 seconds. If a test takes longer to run, we consider it a michael@0: // failure. michael@0: // michael@0: // We put the code for timing a test in a test fixture called michael@0: // "QuickTest". QuickTest is intended to be the super fixture that michael@0: // other fixtures derive from, therefore there is no test case with michael@0: // the name "QuickTest". This is OK. michael@0: // michael@0: // Later, we will derive multiple test fixtures from QuickTest. michael@0: class QuickTest : public testing::Test { michael@0: protected: michael@0: // Remember that SetUp() is run immediately before a test starts. michael@0: // This is a good place to record the start time. michael@0: virtual void SetUp() { michael@0: start_time_ = time(NULL); michael@0: } michael@0: michael@0: // TearDown() is invoked immediately after a test finishes. Here we michael@0: // check if the test was too slow. michael@0: virtual void TearDown() { michael@0: // Gets the time when the test finishes michael@0: const time_t end_time = time(NULL); michael@0: michael@0: // Asserts that the test took no more than ~5 seconds. Did you michael@0: // know that you can use assertions in SetUp() and TearDown() as michael@0: // well? michael@0: EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long."; michael@0: } michael@0: michael@0: // The UTC time (in seconds) when the test starts michael@0: time_t start_time_; michael@0: }; michael@0: michael@0: michael@0: // We derive a fixture named IntegerFunctionTest from the QuickTest michael@0: // fixture. All tests using this fixture will be automatically michael@0: // required to be quick. michael@0: class IntegerFunctionTest : public QuickTest { michael@0: // We don't need any more logic than already in the QuickTest fixture. michael@0: // Therefore the body is empty. michael@0: }; michael@0: michael@0: michael@0: // Now we can write tests in the IntegerFunctionTest test case. michael@0: michael@0: // Tests Factorial() michael@0: TEST_F(IntegerFunctionTest, Factorial) { michael@0: // Tests factorial of negative numbers. michael@0: EXPECT_EQ(1, Factorial(-5)); michael@0: EXPECT_EQ(1, Factorial(-1)); michael@0: EXPECT_GT(Factorial(-10), 0); michael@0: michael@0: // Tests factorial of 0. michael@0: EXPECT_EQ(1, Factorial(0)); michael@0: michael@0: // Tests factorial of positive numbers. michael@0: EXPECT_EQ(1, Factorial(1)); michael@0: EXPECT_EQ(2, Factorial(2)); michael@0: EXPECT_EQ(6, Factorial(3)); michael@0: EXPECT_EQ(40320, Factorial(8)); michael@0: } michael@0: michael@0: michael@0: // Tests IsPrime() michael@0: TEST_F(IntegerFunctionTest, IsPrime) { michael@0: // Tests negative input. michael@0: EXPECT_FALSE(IsPrime(-1)); michael@0: EXPECT_FALSE(IsPrime(-2)); michael@0: EXPECT_FALSE(IsPrime(INT_MIN)); michael@0: michael@0: // Tests some trivial cases. michael@0: EXPECT_FALSE(IsPrime(0)); michael@0: EXPECT_FALSE(IsPrime(1)); michael@0: EXPECT_TRUE(IsPrime(2)); michael@0: EXPECT_TRUE(IsPrime(3)); michael@0: michael@0: // Tests positive input. michael@0: EXPECT_FALSE(IsPrime(4)); michael@0: EXPECT_TRUE(IsPrime(5)); michael@0: EXPECT_FALSE(IsPrime(6)); michael@0: EXPECT_TRUE(IsPrime(23)); michael@0: } michael@0: michael@0: michael@0: // The next test case (named "QueueTest") also needs to be quick, so michael@0: // we derive another fixture from QuickTest. michael@0: // michael@0: // The QueueTest test fixture has some logic and shared objects in michael@0: // addition to what's in QuickTest already. We define the additional michael@0: // stuff inside the body of the test fixture, as usual. michael@0: class QueueTest : public QuickTest { michael@0: protected: michael@0: virtual void SetUp() { michael@0: // First, we need to set up the super fixture (QuickTest). michael@0: QuickTest::SetUp(); michael@0: michael@0: // Second, some additional setup for this fixture. michael@0: q1_.Enqueue(1); michael@0: q2_.Enqueue(2); michael@0: q2_.Enqueue(3); michael@0: } michael@0: michael@0: // By default, TearDown() inherits the behavior of michael@0: // QuickTest::TearDown(). As we have no additional cleaning work michael@0: // for QueueTest, we omit it here. michael@0: // michael@0: // virtual void TearDown() { michael@0: // QuickTest::TearDown(); michael@0: // } michael@0: michael@0: Queue q0_; michael@0: Queue q1_; michael@0: Queue q2_; michael@0: }; michael@0: michael@0: michael@0: // Now, let's write tests using the QueueTest fixture. michael@0: michael@0: // Tests the default constructor. michael@0: TEST_F(QueueTest, DefaultConstructor) { michael@0: EXPECT_EQ(0u, q0_.Size()); michael@0: } michael@0: michael@0: // Tests Dequeue(). michael@0: TEST_F(QueueTest, Dequeue) { michael@0: int* n = q0_.Dequeue(); michael@0: EXPECT_TRUE(n == NULL); michael@0: michael@0: n = q1_.Dequeue(); michael@0: EXPECT_TRUE(n != NULL); michael@0: EXPECT_EQ(1, *n); michael@0: EXPECT_EQ(0u, q1_.Size()); michael@0: delete n; michael@0: michael@0: n = q2_.Dequeue(); michael@0: EXPECT_TRUE(n != NULL); michael@0: EXPECT_EQ(2, *n); michael@0: EXPECT_EQ(1u, q2_.Size()); michael@0: delete n; michael@0: } michael@0: michael@0: // If necessary, you can derive further test fixtures from a derived michael@0: // fixture itself. For example, you can derive another fixture from michael@0: // QueueTest. Google Test imposes no limit on how deep the hierarchy michael@0: // can be. In practice, however, you probably don't want it to be too michael@0: // deep as to be confusing.