Repository navigation
Testing Guide
Voltra Engine uses Google Test (GTest) as its testing framework. This guide will help you write and run tests for the engine.
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
# 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[==========] 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.
Tests are located in the tests/ directory:
Voltra-Engine/
โโโ tests/
โ โโโ main_test.cpp # Main test entry & sanity checks
โ โโโ EventTest.cpp # Event system tests
โ โโโ BufferTest.cpp # Renderer buffer tests
โ โโโ LayerStackTest.cpp # Layer system tests
โ โโโ TimestepTest.cpp # Timestep tests
โ โโโ CMakeLists.txt # Test configuration
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);
}#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);
}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);
}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);
}The renderer system includes tests for buffer abstractions in tests/BufferTest.cpp:
#include <gtest/gtest.h>
#include "Renderer/Buffer.hpp"
// Test ShaderDataType size calculations
TEST(BufferLayout, ShaderDataTypeSize) {
EXPECT_EQ(Voltra::ShaderDataTypeSize(Voltra::ShaderDataType::Float), 4);
EXPECT_EQ(Voltra::ShaderDataTypeSize(Voltra::ShaderDataType::Float3), 12);
EXPECT_EQ(Voltra::ShaderDataTypeSize(Voltra::ShaderDataType::Mat4), 64);
}
// Test BufferElement component counts
TEST(BufferLayout, ElementComponentCount) {
Voltra::BufferElement element(Voltra::ShaderDataType::Float3, "Position");
EXPECT_EQ(element.GetComponentCount(), 3);
}
// Test BufferLayout stride and offset calculations
TEST(BufferLayout, LayoutStrideAndOffsets) {
Voltra::BufferLayout layout = {
{ Voltra::ShaderDataType::Float3, "Position" }, // Size: 12, Offset: 0
{ Voltra::ShaderDataType::Float4, "Color" }, // Size: 16, Offset: 12
{ Voltra::ShaderDataType::Float2, "TexCoord" } // Size: 8, Offset: 28
};
const auto& elements = layout.GetElements();
ASSERT_EQ(elements.size(), 3);
EXPECT_EQ(elements[0].Size, 12);
EXPECT_EQ(elements[0].Offset, 0);
EXPECT_EQ(elements[1].Size, 16);
EXPECT_EQ(elements[1].Offset, 12);
EXPECT_EQ(elements[2].Size, 8);
EXPECT_EQ(elements[2].Offset, 28);
EXPECT_EQ(layout.GetStride(), 36); // Total: 12 + 16 + 8
}Running Renderer Tests:
# Run all buffer tests
./VoltraTests --gtest_filter=BufferLayout.*
# Run specific test
./VoltraTests --gtest_filter=BufferLayout.LayoutStrideAndOffsets| 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 |
TEST(StringTest, Comparison) {
std::string str = "Hello";
EXPECT_STREQ("Hello", str.c_str());
EXPECT_STRNE("World", str.c_str());
}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);
}TEST(ExceptionTest, ThrowsException) {
EXPECT_THROW({
throw std::runtime_error("Error");
}, std::runtime_error);
EXPECT_NO_THROW({
int x = 5 + 5;
});
}-
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
}// โ
GOOD: Descriptive names
TEST(WindowTest, CreatesWindowWithDefaultSize)
TEST(WindowTest, HandlesInvalidDimensions)
TEST(LoggerTest, FormatsMessageCorrectly)
// โ BAD: Vague names
TEST(Test1, Test)
TEST(WindowTest, Test2)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) { /* ... */ }// โ 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);
}// โ
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 defaultTEST(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
}// โ 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);
}// โ 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);
}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);
}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
)cmake --build build --config Release
cd build
ctest --output-on-failureWhile Google Test doesn't provide built-in coverage tools, you can use external tools:
# 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_reportUse Visual Studio's built-in code coverage tools or third-party tools like OpenCppCoverage.
# Run only tests matching a filter
.\Release\VoltraTests.exe --gtest_filter=WindowTest.*
# Run a specific test
.\Release\VoltraTests.exe --gtest_filter=WindowTest.DefaultProperties# Show detailed output
.\Release\VoltraTests.exe --gtest_verbose-
Visual Studio: Set
VoltraTestsas startup project and press F5 - VS Code: Add a launch configuration for the test executable
- CLion: Right-click on a test and select "Debug"
For more information on the engine's architecture, see the Architecture page.