Skip to content

Commit 158cac4

Browse files
committed
Make test run and pass through ctest
1 parent d49fb2b commit 158cac4

File tree

5 files changed

+95
-61
lines changed

5 files changed

+95
-61
lines changed

CMakeLists.txt

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,9 @@ file(GLOB PROJ_POISSON_SOURCES "${PROJ_SRC_DIR}/poisson/*.f90")
2020
add_executable("${PROJECT_NAME}-mesh-generator" "${PROJ_MESH_GENERATOR_SOURCES}")
2121
add_executable("${PROJECT_NAME}-poisson" "${PROJ_POISSON_SOURCES}")
2222

23+
enable_testing()
24+
2325
#--------------------------------------#
2426
# test-drive #
2527
#--------------------------------------#
26-
set(test_lib "test-drive")
27-
set(test_pkg "TEST_DRIVE")
28-
set(test_url "https://github.com/fortran-lang/test-drive")
29-
30-
message(STATUS "Retrieving ${test_lib} from ${test_url}")
31-
include(FetchContent)
32-
FetchContent_Declare(
33-
"${test_lib}"
34-
GIT_REPOSITORY "${test_url}"
35-
GIT_TAG "HEAD"
36-
)
37-
FetchContent_MakeAvailable("${test_lib}")
38-
39-
add_library("${test_lib}::${test_lib}" INTERFACE IMPORTED)
40-
target_link_libraries("${test_lib}::${test_lib}" INTERFACE "${test_lib}")
41-
42-
# We need the module directory in the subproject before we finish the configure stage
43-
FetchContent_GetProperties("${test_lib}" SOURCE_DIR "${test_pkg}_SOURCE_DIR")
44-
FetchContent_GetProperties("${test_lib}" BINARY_DIR "${test_pkg}_BINARY_DIR")
45-
if(NOT EXISTS "${${test_pkg}_BINARY_DIR}/include")
46-
make_directory("${${test_pkg}_BINARY_DIR}/include")
47-
endif()
48-
49-
# Define test files
50-
set(PROJ_TEST_DRIVE_DIR "${PROJECT_SOURCE_DIR}/testing/test-drive")
51-
file(GLOB PROJ_TEST_DRIVE_SOURCES "${PROJ_TEST_DRIVE_DIR}/tests/*.f90")
52-
53-
# Filter out the main.f90 files. We can only have one main() function in our tests
54-
set(PROJ_POISSON_SOURCES_EXC_MAIN ${PROJ_POISSON_SOURCES})
55-
set(PROJ_MESH_GENERATOR_SOURCES_EXC_MAIN ${PROJ_MESH_GENERATOR_SOURCES})
56-
list(FILTER PROJ_POISSON_SOURCES_EXC_MAIN EXCLUDE REGEX ".*main.f90")
57-
list(FILTER PROJ_MESH_GENERATOR_SOURCES_EXC_MAIN EXCLUDE REGEX ".*main.f90")
58-
59-
# Define a list of tests within the test-drive directory
60-
set(
61-
test-drive-tests
62-
"mesh_generator"
63-
"poisson"
64-
)
65-
foreach(t IN LISTS test-drive-tests)
66-
string(MAKE_C_IDENTIFIER ${t} t)
67-
list(APPEND test-drive-srcs "${PROJ_TEST_DRIVE_DIR}/test_${t}.f90")
68-
endforeach()
69-
70-
add_executable(
71-
"${PROJECT_NAME}-test-drive"
72-
"${PROJ_TEST_DRIVE_DIR}/main.f90"
73-
"${PROJ_TEST_DRIVE_SOURCES}"
74-
"${PROJ_POISSON_SOURCES_EXC_MAIN}"
75-
"${PROJ_MESH_GENERATOR_SOURCES_EXC_MAIN}"
76-
)
77-
target_link_libraries(
78-
"${PROJECT_NAME}-test-drive"
79-
PRIVATE
80-
"test-drive::test-drive"
81-
)
82-
83-
foreach(t IN LISTS test-drive-tests)
84-
add_test("${PROJECT_NAME}/${t}" "${PROJECT_NAME}-test-drive" "${t}")
85-
endforeach()
28+
add_subdirectory("testing/test-drive")

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,7 @@ This will produce executables for the two src codes, `fortran-tooling-mesh-gener
5858
```sh
5959
./build/fortran-tooling-poisson # then respond to prompt with the mesh name, likely to be `square_mesh`
6060
```
61+
62+
## Running the tests
63+
64+
To run the tests run `ctest` from within the `build` directory.

testing/test-drive/CMakeLists.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Install the test-drive dependency
2+
set(test_lib "test-drive")
3+
set(test_pkg "TEST_DRIVE")
4+
set(test_url "https://github.com/fortran-lang/test-drive")
5+
6+
message(STATUS "Retrieving ${test_lib} from ${test_url}")
7+
include(FetchContent)
8+
FetchContent_Declare(
9+
"${test_lib}"
10+
GIT_REPOSITORY "${test_url}"
11+
GIT_TAG "HEAD"
12+
)
13+
FetchContent_MakeAvailable("${test_lib}")
14+
15+
add_library("${test_lib}::${test_lib}" INTERFACE IMPORTED)
16+
target_link_libraries("${test_lib}::${test_lib}" INTERFACE "${test_lib}")
17+
18+
# We need the module directory in the subproject before we finish the configure stage
19+
FetchContent_GetProperties("${test_lib}" SOURCE_DIR "${test_pkg}_SOURCE_DIR")
20+
FetchContent_GetProperties("${test_lib}" BINARY_DIR "${test_pkg}_BINARY_DIR")
21+
if(NOT EXISTS "${${test_pkg}_BINARY_DIR}/include")
22+
make_directory("${${test_pkg}_BINARY_DIR}/include")
23+
endif()
24+
25+
# Define test files
26+
set(PROJ_TEST_DRIVE_DIR "${PROJECT_SOURCE_DIR}/testing/test-drive")
27+
file(GLOB PROJ_TEST_DRIVE_SOURCES "${PROJ_TEST_DRIVE_DIR}/tests/*.f90")
28+
29+
# Filter out the main.f90 files. We can only have one main() function in our tests
30+
set(PROJ_POISSON_SOURCES_EXC_MAIN ${PROJ_POISSON_SOURCES})
31+
set(PROJ_MESH_GENERATOR_SOURCES_EXC_MAIN ${PROJ_MESH_GENERATOR_SOURCES})
32+
list(FILTER PROJ_POISSON_SOURCES_EXC_MAIN EXCLUDE REGEX ".*main.f90")
33+
list(FILTER PROJ_MESH_GENERATOR_SOURCES_EXC_MAIN EXCLUDE REGEX ".*main.f90")
34+
35+
# Unit testing
36+
set(
37+
tests
38+
"mesh_generator"
39+
)
40+
set(
41+
test-srcs
42+
"main.f90"
43+
)
44+
foreach(t IN LISTS tests)
45+
string(MAKE_C_IDENTIFIER ${t} t)
46+
list(APPEND test-srcs "${PROJ_TEST_DRIVE_DIR}/test_${t}.f90")
47+
endforeach()
48+
49+
add_executable(
50+
"test_${PROJECT_NAME}-test-drive"
51+
"${test-srcs}"
52+
"${PROJ_TEST_DRIVE_SOURCES}"
53+
"${PROJ_POISSON_SOURCES_EXC_MAIN}"
54+
"${PROJ_MESH_GENERATOR_SOURCES_EXC_MAIN}"
55+
)
56+
target_link_libraries(
57+
"test_${PROJECT_NAME}-test-drive"
58+
PRIVATE
59+
"test-drive::test-drive"
60+
)
61+
62+
foreach(t IN LISTS tests)
63+
add_test("${PROJECT_NAME}-test-drive/${t}" "test_${PROJECT_NAME}-test-drive" "${t}")
64+
endforeach()

testing/test-drive/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
# test-drive
22
This project offers a lightweight, procedural unit testing framework based on nothing but standard Fortran. Integration with [meson](https://mesonbuild.com/), [cmake](https://cmake.org/) and [Fortran package manager (fpm)](https://github.com/fortran-lang/fpm) is available. Alternatively, the testdrive.F90 source file can be redistributed in the project's testsuite as well.
33

4+
## Running the tests
5+
6+
The test-drive tests will run with the rest of the [tests](../README.md#running-the-tests) in the repo.
7+
8+
There is a known issue that the tests for test-drive itself will also be ran by `ctest` as shown below
9+
```sh
10+
$ ctest
11+
Test project /Users/connoraird/work/fortran-tooling/build
12+
Start 1: fortran-tooling-test-drive/mesh_generator
13+
1/4 Test #1: fortran-tooling-test-drive/mesh_generator ... Passed 0.33 sec
14+
Start 2: test-drive/all-tests
15+
2/4 Test #2: test-drive/all-tests ........................ Passed 0.33 sec
16+
Start 3: test-drive/check
17+
3/4 Test #3: test-drive/check ............................ Passed 0.01 sec
18+
Start 4: test-drive/select
19+
4/4 Test #4: test-drive/select ........................... Passed 0.01 sec
20+
21+
100% tests passed, 0 tests failed out of 4
22+
23+
Total Test time (real) = 0.69 sec
24+
```
25+
26+
427
## Features matrix
528

629
Compilers tested: gfortran (homebrew)

testing/test-drive/tests/test_mesh_generator.f90 renamed to testing/test-drive/test_mesh_generator.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module test_mesh_generator
22
use, intrinsic :: iso_fortran_env
3-
use testdrive, only : new_unittest, unittest_type, error_type, check
3+
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
44

55
use mesh_generator
66

0 commit comments

Comments
 (0)