Skip to content

Commit 2ca5fc8

Browse files
committed
Add unit testing support and update README with testing instructions
1 parent d77d49e commit 2ca5fc8

5 files changed

Lines changed: 69 additions & 7 deletions

File tree

.github/workflows/build-and-release.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ jobs:
2929
run: |
3030
make all
3131
32+
- name: Unit testing
33+
run: |
34+
make unittest
35+
3236
- name: Create GitHub Release
3337
id: create_release
3438
uses: actions/create-release@v1

.github/workflows/pr-check-build.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ jobs:
2626
- name: Build all targets
2727
run: |
2828
make all
29-
29+
30+
- name: Unit testing
31+
run: |
32+
make unittest

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ project(std2 LANGUAGES CXX)
66
set(CMAKE_CXX_STANDARD 23)
77
set(CMAKE_CXX_STANDARD_REQUIRED ON)
88

9-
# Enable testing
10-
enable_testing()
9+
# Enable testing globally if TEST=true
10+
if(UNITTEST)
11+
enable_testing()
12+
endif()
1113

1214
# Include FetchContent for downloading dependencies
1315
include(FetchContent)

Makefile

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
# Convenience Makefile for building std2 library components
22

33
BUILD_DIR = build
4+
UNITTEST ?= false
45

5-
.PHONY: all clean memory vector std2
6+
.PHONY: all clean memory vector std2 unittest
67

78
all: $(BUILD_DIR)
89
@cd $(BUILD_DIR) && cmake .. && cmake --build .
10+
@if [ "$(UNITTEST)" = "true" ]; then \
11+
cd $(BUILD_DIR) && ctest --output-on-failure; \
12+
fi
913

1014
$(BUILD_DIR):
1115
@mkdir -p $(BUILD_DIR)
1216

17+
# Build targets with optional testing
1318
memory: $(BUILD_DIR)
14-
@cd $(BUILD_DIR) && cmake .. && cmake --build . --target memory
19+
@cd $(BUILD_DIR) && cmake .. -DTEST=$(UNITTEST) && cmake --build . --target memory memory_tests
20+
@if [ "$(UNITTEST)" = "true" ]; then \
21+
cd $(BUILD_DIR) && ctest --output-on-failure -R "UniquePointerTest"; \
22+
fi
1523

1624
vector: $(BUILD_DIR)
1725
@cd $(BUILD_DIR) && cmake .. && cmake --build . --target vector
26+
@if [ "$(UNITTEST)" = "true" ]; then \
27+
cd $(BUILD_DIR) && ctest --output-on-failure -R "^vector"; \
28+
fi
1829

1930
std2: $(BUILD_DIR)
2031
@cd $(BUILD_DIR) && cmake .. && cmake --build . --target std2
32+
@if [ "$(UNITTEST)" = "true" ]; then \
33+
cd $(BUILD_DIR) && ctest --output-on-failure -R "^std2"; \
34+
fi
2135

36+
# Run all tests explicitly
37+
unittest: $(BUILD_DIR)
38+
@cd $(BUILD_DIR) && ctest --output-on-failure
39+
40+
# Clean target
2241
clean:
2342
@rm -rf $(BUILD_DIR)

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,45 @@ git push origin --tags
2424
```
2525
> Note the tag version must be higher than the current tag
2626
27-
# Run with main to test
27+
# Unit testing
28+
This project uses Google Test framework for unit testing. Each module (memory, vector, etc.) has its own test suite in its `tests/` directory.
29+
30+
## Running tests
31+
You can run tests in several ways:
32+
33+
1. Run all tests without rebuilding:
34+
```bash
35+
make unittest
36+
```
37+
38+
2. Run tests for a specific module:
2839
```bash
29-
./test_memory
40+
make memory TEST=true # Run memory module tests only
41+
make vector TEST=true # Run vector module tests only
3042
```
3143

44+
3. Run all tests:
45+
```bash
46+
make all UNITTEST=true
47+
```
48+
49+
## Test Structure
50+
Each module follows this test structure:
51+
- Basic functionality tests
52+
- Edge case tests
53+
- Move semantics tests (for modern C++ features)
54+
- Custom behavior tests (e.g., custom deleters)
55+
- Performance benchmarks
56+
57+
## Adding New Tests
58+
1. Create test files in the module's `tests/` directory
59+
2. Name test files as `<component>_test.cpp`
60+
3. Use Google Test macros for assertions:
61+
- `EXPECT_EQ()` for equality
62+
- `EXPECT_NE()` for inequality
63+
- `EXPECT_TRUE()` for boolean conditions
64+
- `EXPECT_THROW()` for exception testing
65+
3266
# C++23 Setup
3367

3468
## Update CMake:

0 commit comments

Comments
 (0)