← Back to Course
Lab 2

Lab 2 - Advanced C++ Testing & Test Fixtures

Lab Repository

yourusername/istqb-lab2-cpp-advanced

Quick Start

git clone https://github.com/yourusername/istqb-lab2-cpp-advanced.git

Lab 2: Advanced C++ Testing with Test Fixtures

Objective

Master test fixtures, parameterized tests, and testing object-oriented C++ code.

Part 1: Test Fixtures for Complex Classes

Bank Account Class

Create src/bank_account.h:

#ifndef BANK_ACCOUNT_H
#define BANK_ACCOUNT_H

#include <string>
#include <vector>

struct Transaction {
    std::string type;  // "deposit" or "withdrawal"
    double amount;
    std::string timestamp;
};

class BankAccount {
private:
    std::string accountNumber;
    double balance;
    std::vector<Transaction> transactions;

public:
    BankAccount(const std::string& accNum, double initialBalance = 0.0);
    
    void deposit(double amount);
    bool withdraw(double amount);
    double getBalance() const;
    std::vector<Transaction> getTransactionHistory() const;
    void transfer(BankAccount& toAccount, double amount);
};

#endif

Test Fixture

Create tests/bank_account_test.cpp:

#include <gtest/gtest.h>
#include "bank_account.h"

class BankAccountTest : public ::testing::Test {
protected:
    void SetUp() override {
        // Runs before each test
        account = new BankAccount("ACC001", 1000.0);
    }

    void TearDown() override {
        // Runs after each test
        delete account;
    }

    BankAccount* account;
};

TEST_F(BankAccountTest, InitialBalanceIsCorrect) {
    EXPECT_DOUBLE_EQ(account->getBalance(), 1000.0);
}

TEST_F(BankAccountTest, DepositIncreasesBalance) {
    account->deposit(500.0);
    EXPECT_DOUBLE_EQ(account->getBalance(), 1500.0);
}

TEST_F(BankAccountTest, WithdrawDecreasesBalance) {
    EXPECT_TRUE(account->withdraw(300.0));
    EXPECT_DOUBLE_EQ(account->getBalance(), 700.0);
}

TEST_F(BankAccountTest, CannotWithdrawMoreThanBalance) {
    EXPECT_FALSE(account->withdraw(1500.0));
    EXPECT_DOUBLE_EQ(account->getBalance(), 1000.0);
}

TEST_F(BankAccountTest, TransactionHistoryIsRecorded) {
    account->deposit(200.0);
    account->withdraw(100.0);
    
    auto history = account->getTransactionHistory();
    EXPECT_EQ(history.size(), 2);
    EXPECT_EQ(history[0].type, "deposit");
    EXPECT_DOUBLE_EQ(history[0].amount, 200.0);
}

Part 2: Parameterized Tests

class DepositTest : public ::testing::TestWithParam<std::pair<double, double>> {
protected:
    BankAccount account{"TEST", 1000.0};
};

TEST_P(DepositTest, DepositVariousAmounts) {
    auto [depositAmount, expectedBalance] = GetParam();
    account.deposit(depositAmount);
    EXPECT_DOUBLE_EQ(account.getBalance(), expectedBalance);
}

INSTANTIATE_TEST_SUITE_P(
    DepositTestCases,
    DepositTest,
    ::testing::Values(
        std::make_pair(100.0, 1100.0),
        std::make_pair(0.01, 1000.01),
        std::make_pair(1000000.0, 1001000.0)
    )
);

Deliverables

  • Implement BankAccount class
  • Write 20+ unit tests
  • Achieve >90% code coverage
  • Test report with results

ā±ļø Time: 4-5 hours