Google Tests

This commit is contained in:
cdemeyer-teachx
2025-08-14 10:19:03 +09:00
parent e2cb19b92f
commit 1c1e7f8d51
19 changed files with 500 additions and 2 deletions

75
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,75 @@
# Tests CMakeLists.txt
include(FetchContent)
# Download and configure Google Test
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/v1.14.0.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
# Prevent overriding the parent project's compiler/linker on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Make Google Test available
FetchContent_MakeAvailable(googletest)
# Include Google Test modules
include(GoogleTest)
# Create test executable for unit tests
add_executable(unit_tests
unit/main.cpp
)
# Link with Google Test and our library
target_link_libraries(unit_tests
PRIVATE
GTest::gtest_main
GTest::gmock_main
PokemonSim::pokemon_battle_sim
)
# Set up test discovery
gtest_discover_tests(unit_tests
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
PROPERTIES
LABELS "unit"
)
# Create test executable for integration tests
add_executable(integration_tests
integration/main.cpp
)
# Link with Google Test and our library
target_link_libraries(integration_tests
PRIVATE
GTest::gtest_main
GTest::gmock_main
PokemonSim::pokemon_battle_sim
)
# Set up test discovery
gtest_discover_tests(integration_tests
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
PROPERTIES
LABELS "integration"
)
# Include directories for tests
target_include_directories(unit_tests
PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
)
target_include_directories(integration_tests
PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
)
# Add test subdirectories
add_subdirectory(unit)
add_subdirectory(integration)

View File

@@ -38,10 +38,49 @@ tests/
## Testing Framework
- **Google Test (gtest)**: Primary testing framework
- **Google Mock (gmock)**: For mocking dependencies
- **Google Test (gtest)**: Primary testing framework - automatically downloaded via FetchContent
- **Google Mock (gmock)**: For mocking dependencies - included with Google Test
- **Test Coverage**: Aim for >90% code coverage
- **Continuous Integration**: Tests run on every commit
- **Version**: Google Test v1.14.0
## Running Tests
### Build and Run
```bash
# Configure and build
cmake -B build -S .
cmake --build build
# Run all tests
cmake --build build --target test
# Run specific test types
cd build && ctest -L unit # Unit tests only
cd build && ctest -L integration # Integration tests only
# Run with verbose output
cd build && ctest -V
```
### Test Structure (Now Implemented)
```
tests/
├── unit/ # Unit tests for individual components
│ ├── main.cpp # Unit test entry point
│ ├── CMakeLists.txt # Unit test build config
│ └── core/ # Core system tests
│ ├── CMakeLists.txt
│ └── test_example.cpp # Example test (ready to run)
├── integration/ # Integration tests
│ ├── main.cpp # Integration test entry point
│ └── CMakeLists.txt # Integration test build config
├── fixtures/ # Test data and fixtures
├── helpers/ # Test helper utilities
└── CMakeLists.txt # Main test build configuration
```
## Test Categories

View File

@@ -0,0 +1,18 @@
# Integration tests CMakeLists.txt
# Collect all test source files
file(GLOB_RECURSE INTEGRATION_TEST_SOURCES "*.cpp" "*.cc" "*.cxx")
# Remove main.cpp from the sources list if it exists
list(REMOVE_ITEM INTEGRATION_TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
# Add integration test sources to the integration_tests executable
if(INTEGRATION_TEST_SOURCES)
target_sources(integration_tests PRIVATE ${INTEGRATION_TEST_SOURCES})
endif()
# Example of how to add specific test files:
# target_sources(integration_tests PRIVATE
# test_battle_integration.cpp
# test_full_game_flow.cpp
# )

View File

@@ -0,0 +1,11 @@
#include <gtest/gtest.h>
// Main test runner for integration tests
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
// Set up any global test configuration here
// Integration tests might need different setup than unit tests
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,9 @@
# Unit tests CMakeLists.txt
# Add unit test subdirectories
add_subdirectory(core)
# You can add more unit test subdirectories here as needed:
# add_subdirectory(math)
# add_subdirectory(utils)
# add_subdirectory(battle)

View File

@@ -0,0 +1,18 @@
# Core unit tests CMakeLists.txt
# Collect all test source files
file(GLOB_RECURSE CORE_TEST_SOURCES "*.cpp" "*.cc" "*.cxx")
# Remove main.cpp from the sources list if it exists
list(REMOVE_ITEM CORE_TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
# Add core test sources to the unit_tests executable
if(CORE_TEST_SOURCES)
target_sources(unit_tests PRIVATE ${CORE_TEST_SOURCES})
endif()
# Example of how to add specific test files:
# target_sources(unit_tests PRIVATE
# test_example.cpp
# test_another_example.cpp
# )

View File

@@ -0,0 +1,75 @@
#include <gtest/gtest.h>
// Example test case demonstrating Google Test usage
class ExampleTest : public ::testing::Test {
protected:
void SetUp() override {
// Set up test fixtures here
}
void TearDown() override {
// Clean up test fixtures here
}
};
// Basic assertion test
TEST_F(ExampleTest, BasicAssertions) {
// Basic equality assertions
EXPECT_EQ(2 + 2, 4);
EXPECT_NE(2 + 2, 5);
// Boolean assertions
EXPECT_TRUE(true);
EXPECT_FALSE(false);
// String assertions
EXPECT_STREQ("hello", "hello");
EXPECT_STRNE("hello", "world");
}
// Test with parameters
TEST(ParameterizedTest, Addition) {
EXPECT_EQ(1 + 1, 2);
EXPECT_EQ(10 + 5, 15);
EXPECT_EQ(-1 + 1, 0);
}
// Test that demonstrates failure (using TEST_F to match the fixture)
TEST_F(ExampleTest, ThisWillFail) {
// This test will fail to demonstrate the failure output
// EXPECT_EQ(2 + 2, 5); // Uncomment this line to see a failing test
}
// Example of how you might test a Pokemon battle simulator component
// This demonstrates testing the placeholder implementation we created
#include "pokemon_battle_sim.h"
TEST(PokemonTest, PokemonCreation) {
PokemonSim::Pokemon pikachu("Pikachu", 100);
// Test basic properties
EXPECT_EQ(pikachu.getName(), "Pikachu");
EXPECT_EQ(pikachu.getHealth(), 100);
}
TEST(PokemonTest, PokemonHealthModification) {
PokemonSim::Pokemon charizard("Charizard", 150);
EXPECT_EQ(charizard.getHealth(), 150);
charizard.setHealth(100);
EXPECT_EQ(charizard.getHealth(), 100);
}
TEST(PokemonTest, BattleSimulation) {
PokemonSim::Pokemon pokemon1("Pokemon1", 50);
PokemonSim::Pokemon pokemon2("Pokemon2", 30);
// Pokemon1 should win because it has more health
bool result = PokemonSim::simulateBattle(pokemon1, pokemon2);
EXPECT_TRUE(result); // Pokemon1 wins
// Both Pokemon should have taken damage
EXPECT_LT(pokemon1.getHealth(), 50);
EXPECT_LE(pokemon2.getHealth(), 0);
}

11
tests/unit/main.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include <gtest/gtest.h>
// Main test runner for unit tests
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
// Set up any global test configuration here
// For example, you can set test filters, output format, etc.
return RUN_ALL_TESTS();
}