This directory contains a comprehensive test framework for the OpenDAL C++ bindings, designed to match the sophistication and organization of test frameworks in other language bindings (Python, Go, Node.js).
tests/ ├── framework/ # Test framework utilities and base classes │ └── test_framework.hpp # Core framework header ├── behavior/ # Behavior-based tests (similar to Go tests) │ ├── read_test.cpp # Read operation tests │ ├── write_test.cpp # Write operation tests │ ├── delete_test.cpp # Delete operation tests │ ├── list_test.cpp # List operation tests │ └── async_read_test.cpp # Async read tests (if OPENDAL_ENABLE_ASYNC) ├── benchmark/ # Performance benchmarks │ └── benchmark_test.cpp # Comprehensive benchmarks ├── test_main.cpp # Main test runner ├── basic_test.cpp # Legacy basic tests (for compatibility) ├── async_test.cpp # Legacy async tests (for compatibility) └── README.md # This file
# Configure with testing enabled cmake -DOPENDAL_ENABLE_TESTING=ON -DOPENDAL_DEV=ON .. # Build tests make opendal_cpp_test # Run all tests ./opendal_cpp_test # Run specific test suites ./opendal_cpp_test --gtest_filter="ReadBehaviorTest.*" ./opendal_cpp_test --gtest_filter="*Benchmark*"
The test framework supports configuration through environment variables:
# Set the service to test (default: memory) export OPENDAL_TEST=memory # Service-specific configuration export OPENDAL_MEMORY_ROOT=/tmp/opendal_test # Disable random root (for deterministic paths) export OPENDAL_DISABLE_RANDOM_ROOT=true # For other services (example with filesystem) export OPENDAL_TEST=fs export OPENDAL_FS_ROOT=/tmp/opendal_fs_test
When built with async support (-DOPENDAL_ENABLE_ASYNC=ON
):
# Build with async support cmake -DOPENDAL_ENABLE_TESTING=ON -DOPENDAL_ENABLE_ASYNC=ON -DOPENDAL_DEV=ON .. make opendal_cpp_test # Run async tests ./opendal_cpp_test --gtest_filter="Async*"
#include "../framework/test_framework.hpp" namespace opendal::test { class MyBehaviorTest : public OpenDALTest { protected: void SetUp() override { OpenDALTest::SetUp(); // Additional setup if needed } }; OPENDAL_TEST_F(MyBehaviorTest, TestSomething) { auto path = random_path(); auto content = random_string(100); // Your test logic here op_.write(path, content); auto result = op_.read(path); EXPECT_EQ(result, content); } } // namespace opendal::test
#ifdef OPENDAL_ENABLE_ASYNC class MyAsyncBehaviorTest : public AsyncOpenDALTest { // Similar structure but using co_await }; OPENDAL_TEST_F(MyAsyncBehaviorTest, TestAsyncSomething) { cppcoro::sync_wait([&]() -> cppcoro::task<void> { auto path = random_path(); auto content = random_bytes(100); co_await op_->write(path, content); auto result = co_await op_->read(path); EXPECT_EQ(result, content); co_return; }()); } #endif
class MyBenchmarkTest : public BenchmarkTest { // Inherit from BenchmarkTest }; OPENDAL_TEST_F(MyBenchmarkTest, BenchmarkMyOperation) { benchmark_operation( "My operation description", [&]() { // Operation to benchmark auto path = random_path(); op_.write(path, random_string(1024)); }, 100 // iterations ); }
OpenDALTest
: Base class for sync testsAsyncOpenDALTest
: Base class for async tests (when enabled)BenchmarkTest
: Base class for performance testsrandom_path()
: Generate random file pathrandom_dir_path()
: Generate random directory pathrandom_string(size)
: Generate random string datarandom_bytes(size)
: Generate random binary dataPerformanceTimer
: High-resolution timingbenchmark_operation()
: Automated benchmarking with statisticsTestConfig::instance()
: Access test configurationinitialize_test_framework()
: Initialize framework (called by main)- name: Run C++ Tests run: | cd bindings/cpp mkdir build && cd build cmake -DOPENDAL_ENABLE_TESTING=ON -DOPENDAL_DEV=ON .. make opendal_cpp_test ./opendal_cpp_test --gtest_output=xml:test_results.xml
# Run only basic functionality tests ./opendal_cpp_test --gtest_filter="*BehaviorTest*" --gtest_filter="-*Benchmark*" # Run only benchmarks ./opendal_cpp_test --gtest_filter="*Benchmark*" # Run only async tests ./opendal_cpp_test --gtest_filter="*Async*" # Run with verbose output ./opendal_cpp_test --gtest_verbose
This C++ test framework provides equivalent functionality to:
When adding new tests:
Tests fail with “No service configured”
OPENDAL_TEST
environment variableAsync tests don't compile
-DOPENDAL_ENABLE_ASYNC=ON
Permission errors
Memory issues
-DOPENDAL_ENABLE_ADDRESS_SANITIZER=ON