dotenv
includes tests to verify the functionality of loading and retrieving environment variables. These tests are written using Google Test and can be run using CTest.
To run the tests, make sure you have Google Test installed or use CMake to download it automatically. Here’s an example CMake setup that downloads Google Test as an external dependency for testing purposes.
-
In your project directory, create a
tests/
folder with aCMakeLists.txt
file. -
Inside
tests/CMakeLists.txt
, add the following:# Download and configure Google Test include(FetchContent) FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/release-1.12.1.zip ) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest) # Create a test executable add_executable(test_dotenv test_dotenv.cpp) target_link_libraries(test_dotenv gtest_main) add_test(NAME dotenvTests COMMAND test_dotenv)
-
In your main
CMakeLists.txt
, enable testing and add thetests
directory:enable_testing() add_subdirectory(tests)
Create a file test_dotenv.cpp
inside the tests
directory to define test cases. Here’s an example:
#include "dotenv.h"
#include <gtest/gtest.h>
TEST(dotenvTest, LoadFromFile) {
dotenv env(".env");
EXPECT_EQ(env.get("DB_HOST", "default_host"), "127.0.0.1");
EXPECT_EQ(env.get("DB_USER", "default_user"), "admin");
EXPECT_EQ(env.get("DB_PASSWORD", "default_password"), "secret");
}
TEST(dotenvTest, DefaultValues) {
dotenv env(".env");
EXPECT_EQ(env.get("NON_EXISTENT_VAR", "default_value"), "default_value");
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
-
Build the Project with CMake, which will automatically set up the test executable:
mkdir build && cd build cmake .. make
-
Run Tests using CTest:
ctest
CTest will automatically discover and execute the tests, providing a summary of the results.
CTest will display output indicating which tests passed or failed. Each test case will show a success message or provide details if a test failed. This feedback ensures that the dotenv
library functions as expected across different scenarios.