Skip to content

Testing Guide

Rafa San Pablo Gonzรกlez edited this page Dec 23, 2025 · 5 revisions

๐Ÿงช Testing Guide

Voltra Engine uses Google Test (GTest) as its testing framework. This guide will help you write and run tests for the engine.

๐ŸŽฏ Overview

Testing is crucial for maintaining code quality and preventing regressions. Voltra Engine's test suite includes:

  • Unit Tests: Test individual components in isolation
  • Integration Tests: Test how components work together
  • Automated Testing: Run tests automatically via CMake/CTest

๐Ÿš€ Quick Start

Running Tests

# Build the project first
cmake -S . -B build
cmake --build build --config Release

# Run tests via CTest
cd build
ctest --output-on-failure

# Or run the test executable directly
.\Release\VoltraTests.exe  # Windows
./VoltraTests              # Linux/macOS

Expected Output

[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from BasicTest
[ RUN      ] BasicTest.AssertionTrue
[       OK ] BasicTest.AssertionTrue (0 ms)
[----------] 1 test from BasicTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

๐Ÿ“ Test Structure

Tests are located in the tests/ directory:

Voltra-Engine/
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ main_test.cpp    # Main test file
โ””โ”€โ”€ CMakeLists.txt       # Test configuration

Current Test File

The tests/main_test.cpp file contains a basic test to verify the testing framework is working:

#include <gtest/gtest.h>

// Basic test to verify Google Test is working
TEST(BasicTest, AssertionTrue) {
    EXPECT_TRUE(true);
}

โœ๏ธ Writing Tests

Basic Test Structure

#include <gtest/gtest.h>

// Test syntax: TEST(TestSuiteName, TestName)
TEST(MathTest, Addition) {
    EXPECT_EQ(2 + 2, 4);
    EXPECT_EQ(10 + 5, 15);
}

TEST(MathTest, Subtraction) {
    EXPECT_EQ(10 - 5, 5);
    EXPECT_EQ(0 - 5, -5);
}

Testing Engine Components

To test engine components, include the necessary headers:

#include <gtest/gtest.h>
#include "Core/Window.hpp"

TEST(WindowTest, DefaultProperties) {
    Voltra::Window::Properties props;
    
    EXPECT_EQ(props.Title, "Voltra Engine");
    EXPECT_EQ(props.Width, 1280);
    EXPECT_EQ(props.Height, 720);
}

TEST(WindowTest, CustomProperties) {
    Voltra::Window::Properties props("Test Window", 800, 600);
    
    EXPECT_EQ(props.Title, "Test Window");
    EXPECT_EQ(props.Width, 800);
    EXPECT_EQ(props.Height, 600);
}

Test Fixtures

For more complex tests that require setup and teardown:

#include <gtest/gtest.h>
#include "Core/Application.hpp"

class ApplicationTest : public ::testing::Test {
protected:
    // Setup: Called before each test
    void SetUp() override {
        // Initialize resources
        Voltra::Log::Init();
    }

    // Teardown: Called after each test
    void TearDown() override {
        // Clean up resources
    }

    // Helper methods
    void HelperFunction() {
        // Shared test logic
    }
};

// Use TEST_F for fixture-based tests
TEST_F(ApplicationTest, Initialization) {
    // Test code here
    EXPECT_TRUE(true);
}

TEST_F(ApplicationTest, AnotherTest) {
    HelperFunction();
    EXPECT_TRUE(true);
}

๐Ÿ” Assertion Macros

Basic Assertions

Macro Description
EXPECT_TRUE(condition) Expects condition to be true
EXPECT_FALSE(condition) Expects condition to be false
EXPECT_EQ(val1, val2) Expects val1 == val2
EXPECT_NE(val1, val2) Expects val1 != val2
EXPECT_LT(val1, val2) Expects val1 < val2
EXPECT_LE(val1, val2) Expects val1 <= val2
EXPECT_GT(val1, val2) Expects val1 > val2
EXPECT_GE(val1, val2) Expects val1 >= val2

String Assertions

TEST(StringTest, Comparison) {
    std::string str = "Hello";
    
    EXPECT_STREQ("Hello", str.c_str());
    EXPECT_STRNE("World", str.c_str());
}

Floating Point Assertions

TEST(FloatTest, Comparison) {
    float a = 0.1f + 0.2f;
    float b = 0.3f;
    
    // Use EXPECT_FLOAT_EQ for floating point comparison
    EXPECT_FLOAT_EQ(a, b);
    
    // Or use EXPECT_NEAR with tolerance
    EXPECT_NEAR(a, b, 0.0001f);
}

Exception Assertions

TEST(ExceptionTest, ThrowsException) {
    EXPECT_THROW({
        throw std::runtime_error("Error");
    }, std::runtime_error);
    
    EXPECT_NO_THROW({
        int x = 5 + 5;
    });
}

Fatal vs Non-Fatal

  • EXPECT_*: Non-fatal - test continues even if assertion fails
  • ASSERT_*: Fatal - test stops if assertion fails
TEST(AssertionTest, FatalVsNonFatal) {
    EXPECT_EQ(1, 2);  // Fails, but test continues
    EXPECT_EQ(3, 3);  // This will still run
    
    ASSERT_EQ(1, 2);  // Fails and stops test
    EXPECT_EQ(3, 3);  // This will NOT run
}

๐Ÿ“‹ Test Organization

Naming Conventions

// โœ… GOOD: Descriptive names
TEST(WindowTest, CreatesWindowWithDefaultSize)
TEST(WindowTest, HandlesInvalidDimensions)
TEST(LoggerTest, FormatsMessageCorrectly)

// โŒ BAD: Vague names
TEST(Test1, Test)
TEST(WindowTest, Test2)

Grouping Tests

Group related tests in the same test suite:

// Window creation tests
TEST(WindowTest, DefaultCreation) { /* ... */ }
TEST(WindowTest, CustomCreation) { /* ... */ }
TEST(WindowTest, InvalidCreation) { /* ... */ }

// Window behavior tests
TEST(WindowTest, ShouldCloseInitiallyFalse) { /* ... */ }
TEST(WindowTest, GetWidthReturnsCorrectValue) { /* ... */ }

๐ŸŽฏ Best Practices

1. Test One Thing at a Time

// โŒ BAD: Testing multiple things
TEST(WindowTest, Everything) {
    Voltra::Window::Properties props;
    EXPECT_EQ(props.Width, 1280);
    EXPECT_EQ(props.Height, 720);
    EXPECT_EQ(props.Title, "Voltra Engine");
    // ... many more assertions
}

// โœ… GOOD: Separate tests
TEST(WindowTest, DefaultWidth) {
    Voltra::Window::Properties props;
    EXPECT_EQ(props.Width, 1280);
}

TEST(WindowTest, DefaultHeight) {
    Voltra::Window::Properties props;
    EXPECT_EQ(props.Height, 720);
}

2. Use Descriptive Messages

// โœ… GOOD: Custom failure message
EXPECT_EQ(width, 1280) << "Window width should be 1280 by default";

// Output on failure:
// Expected equality of these values:
//   width
//     Which is: 800
//   1280
// Window width should be 1280 by default

3. Test Edge Cases

TEST(WindowTest, HandlesZeroWidth) {
    Voltra::Window::Properties props("Test", 0, 720);
    // Test how the engine handles invalid input
}

TEST(WindowTest, HandlesNegativeHeight) {
    Voltra::Window::Properties props("Test", 1280, -100);
    // Test how the engine handles invalid input
}

TEST(WindowTest, HandlesVeryLargeSize) {
    Voltra::Window::Properties props("Test", 100000, 100000);
    // Test how the engine handles extreme values
}

4. Keep Tests Fast

// โŒ BAD: Slow test
TEST(SlowTest, WaitsForever) {
    std::this_thread::sleep_for(std::chrono::seconds(10));
    EXPECT_TRUE(true);
}

// โœ… GOOD: Fast test
TEST(FastTest, QuickCheck) {
    EXPECT_TRUE(true);
}

5. Make Tests Independent

// โŒ BAD: Tests depend on each other
int globalCounter = 0;

TEST(DependentTest, First) {
    globalCounter = 5;
}

TEST(DependentTest, Second) {
    EXPECT_EQ(globalCounter, 5);  // Depends on First running!
}

// โœ… GOOD: Independent tests
TEST(IndependentTest, First) {
    int counter = 5;
    EXPECT_EQ(counter, 5);
}

TEST(IndependentTest, Second) {
    int counter = 10;
    EXPECT_EQ(counter, 10);
}

๐Ÿ”ง Adding New Tests

1. Edit tests/main_test.cpp

Add your tests to the existing file:

#include <gtest/gtest.h>
#include "Core/Window.hpp"

// Existing test
TEST(BasicTest, AssertionTrue) {
    EXPECT_TRUE(true);
}

// Your new test
TEST(WindowTest, DefaultProperties) {
    Voltra::Window::Properties props;
    EXPECT_EQ(props.Width, 1280);
    EXPECT_EQ(props.Height, 720);
}

2. Create New Test Files (Optional)

For larger test suites, create separate files:

// tests/window_test.cpp
#include <gtest/gtest.h>
#include "Core/Window.hpp"

TEST(WindowTest, DefaultProperties) {
    // Test code
}

TEST(WindowTest, CustomProperties) {
    // Test code
}

Then update CMakeLists.txt:

# Add new test file
add_executable(VoltraTests 
    tests/main_test.cpp
    tests/window_test.cpp  # Add your new file
)

3. Rebuild and Run

cmake --build build --config Release
cd build
ctest --output-on-failure

๐Ÿ“Š Test Coverage

While Google Test doesn't provide built-in coverage tools, you can use external tools:

On Linux/macOS (with GCC/Clang)

# Build with coverage flags
cmake -S . -B build -DCMAKE_CXX_FLAGS="--coverage"
cmake --build build

# Run tests
cd build
ctest

# Generate coverage report (requires lcov)
lcov --capture --directory . --output-file coverage.info
genhtml coverage.info --output-directory coverage_report

On Windows (with Visual Studio)

Use Visual Studio's built-in code coverage tools or third-party tools like OpenCppCoverage.

๐Ÿ› Debugging Tests

Running Specific Tests

# Run only tests matching a filter
.\Release\VoltraTests.exe --gtest_filter=WindowTest.*

# Run a specific test
.\Release\VoltraTests.exe --gtest_filter=WindowTest.DefaultProperties

Verbose Output

# Show detailed output
.\Release\VoltraTests.exe --gtest_verbose

Debugging in IDE

  1. Visual Studio: Set VoltraTests as startup project and press F5
  2. VS Code: Add a launch configuration for the test executable
  3. CLion: Right-click on a test and select "Debug"

๐Ÿ“š Further Reading


For more information on the engine's architecture, see the Architecture page.

Clone this wiki locally