From 0c84ad123f64180ee165d908c36575075dc4a5eb Mon Sep 17 00:00:00 2001 From: Enzo Evers Date: Sat, 30 Nov 2024 15:51:06 +0100 Subject: [PATCH] test: use test fixtures from the unity test framework --- CoDeLib/Test/CMakeLists.txt | 4 +++ CoDeLib/Test/src/TestDeflateInflateZlib.c | 36 ++++++++++------------- CoDeLib/Test/src/TestDeflateInflateZlib.h | 3 ++ CoDeLib/Test/src/main.c | 22 ++++++++++++++ Scripts/RunTest.py | 30 ++++++++++++------- 5 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 CoDeLib/Test/src/TestDeflateInflateZlib.h create mode 100644 CoDeLib/Test/src/main.c diff --git a/CoDeLib/Test/CMakeLists.txt b/CoDeLib/Test/CMakeLists.txt index 6bba10f3..381cf123 100644 --- a/CoDeLib/Test/CMakeLists.txt +++ b/CoDeLib/Test/CMakeLists.txt @@ -1,4 +1,5 @@ add_executable(CoDeLib_Test + src/main.c src/TestDeflateInflateZlib.c) target_include_directories(CoDeLib_Test PUBLIC @@ -15,6 +16,9 @@ FetchContent_Declare( GIT_TAG 860062d51b2e8a75d150337b63ca2a472840d13c # v2.6.0 EXCLUDE_FROM_ALL ) +# Solution to use `CACHE INTERNAL` found here: https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/4 +# If cache is not used, you will get `Policy CMP0077 is not set: option() honors normal variables.` +set(UNITY_EXTENSION_FIXTURE ON CACHE INTERNAL "") FetchContent_MakeAvailable(Unity) target_link_libraries(CoDeLib_Test PRIVATE unity) diff --git a/CoDeLib/Test/src/TestDeflateInflateZlib.c b/CoDeLib/Test/src/TestDeflateInflateZlib.c index d2e6c8e7..ad12dce0 100644 --- a/CoDeLib/Test/src/TestDeflateInflateZlib.c +++ b/CoDeLib/Test/src/TestDeflateInflateZlib.c @@ -1,4 +1,4 @@ -#include "unity.h" +#include "unity_fixture.h" #include #include #include @@ -7,17 +7,23 @@ #include #include -char *g_pFullPathToBenchmarkTestFiles = NULL; +static char *g_pFullPathToBenchmarkTestFiles = NULL; -void setUp(void) { - // set stuff up here +void SetupTestDeflateInflateZlib(char *pFullPathToBenchmarkTestFiles) { + g_pFullPathToBenchmarkTestFiles = pFullPathToBenchmarkTestFiles; } -void tearDown(void) { - // clean stuff up here +TEST_GROUP(TestDeflateInflateZlib); + +TEST_SETUP(TestDeflateInflateZlib) { + // Nothing +} + +TEST_TEAR_DOWN(TestDeflateInflateZlib) { + // Nothing } -void test_InflateZlibWorkWithDeflateZlib(void) { +TEST(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib) { FILE *pInFile = NULL; FILE *pOutCompressedFile = NULL; FILE *pOutDecompressedFile = NULL; @@ -41,22 +47,12 @@ void test_InflateZlibWorkWithDeflateZlib(void) { TEST_ASSERT_GREATER_THAN(0, GetFileSizeInBytes(pOutDecompressedFile)); TEST_ASSERT_EQUAL(INFLATE_SUCCESS, statusInflate); - FilesAreEqual(pInFile, pOutDecompressedFile); + TEST_ASSERT(FilesAreEqual(pInFile, pOutDecompressedFile)); fclose(pInFile); fclose(pOutCompressedFile); fclose(pOutDecompressedFile); } -int main(int argc, char **argv) { - // TODO: use getopt(...) - if (argc == 1) { - printf("No arguments provided\n"); - return 1; - } else { - g_pFullPathToBenchmarkTestFiles = argv[1]; - } - - UNITY_BEGIN(); - RUN_TEST(test_InflateZlibWorkWithDeflateZlib); - return UNITY_END(); +TEST_GROUP_RUNNER(TestDeflateInflateZlib) { + RUN_TEST_CASE(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib); } \ No newline at end of file diff --git a/CoDeLib/Test/src/TestDeflateInflateZlib.h b/CoDeLib/Test/src/TestDeflateInflateZlib.h new file mode 100644 index 00000000..40ef1b53 --- /dev/null +++ b/CoDeLib/Test/src/TestDeflateInflateZlib.h @@ -0,0 +1,3 @@ +#pragma once + +void SetupTestDeflateInflateZlib(char *pFullPathToBenchmarkTestFiles); \ No newline at end of file diff --git a/CoDeLib/Test/src/main.c b/CoDeLib/Test/src/main.c new file mode 100644 index 00000000..de9603cc --- /dev/null +++ b/CoDeLib/Test/src/main.c @@ -0,0 +1,22 @@ +#include "unity_fixture.h" + +#include "TestDeflateInflateZlib.h" +#include + +static void RunAllTests(void) { RUN_TEST_GROUP(TestDeflateInflateZlib); } + +int main(int argc, const char **argv) { + char *pFullPathToBenchmarkTestFiles; + + // TODO: use getopt(...) + if (argc == 1) { + printf("No arguments provided\n"); + return 1; + } else { + pFullPathToBenchmarkTestFiles = (char *)argv[1]; + } + + SetupTestDeflateInflateZlib(pFullPathToBenchmarkTestFiles); + + return UnityMain(argc, argv, RunAllTests); +} \ No newline at end of file diff --git a/Scripts/RunTest.py b/Scripts/RunTest.py index c6ceee04..f9e96b11 100644 --- a/Scripts/RunTest.py +++ b/Scripts/RunTest.py @@ -80,8 +80,9 @@ def RunTests( buildEnv: EnvironmentConfig.EnvironmentConfiguration, buildConfig: EnvironmentConfig.BuildConfig, testList: List[str], -) -> List[str]: +) -> tuple[bool, List[str]]: RawTestResultsFileNames = [] + success = True BuildTypeString = EnvironmentConfig.BuildConfig.ToCMakeBuildType(buildConfig) targetPlatformString = EnvironmentConfig.Platform.PlatformToOsName( @@ -118,18 +119,19 @@ def RunTests( TestResultsFileRaw = open(RawTestResultsFileNames[-1], "w") - subprocess.run( + result = subprocess.run( TestCommand, shell=True, - check=True, + check=False, stdout=TestResultsFileRaw, ) - PrependTestHeaderInRawResultsFile(testHeader, RawTestResultsFileNames[-1]) - TestResultsFileRaw.close() - return RawTestResultsFileNames + if result.returncode != 0: + success = False + + return (success, RawTestResultsFileNames) # ===================================================================================== @@ -147,13 +149,21 @@ def RunTests( RepositoryRootPath, targetPlatform ) -RawResultFiles = RunTests(BuildEnv, EnvironmentConfig.BuildConfig.DEBUG, TestList) -AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFiles) +[successDebug, RawResultFilesDebug] = RunTests( + BuildEnv, EnvironmentConfig.BuildConfig.DEBUG, TestList +) +AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFilesDebug) -RawResultFiles = RunTests(BuildEnv, EnvironmentConfig.BuildConfig.RELEASE, TestList) -AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFiles) +[successRelease, RawResultFilesRelease] = RunTests( + BuildEnv, EnvironmentConfig.BuildConfig.RELEASE, TestList +) +AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFilesRelease) # Print the content of BenchmarkResultFileName to the console print("\n") with open(TestResultsFileName, "r") as file: print(file.read()) + +if not successDebug or not successRelease: + print("Test failed") + exit(1)