Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test infrastructure setup #144

Merged
merged 18 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/workflows/code-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: code-coverage

on:
push:
branches:
- development
- main
workflow_dispatch:
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
cancel-in-progress: true

jobs:
gcc:
runs-on: ubuntu-latest
steps:
- name: Checkout atmospheric_physics
uses: actions/checkout@v4

- name: Install dependencies
run: |
sudo apt update && sudo apt -y install libopenmpi-dev openmpi-bin

- name: Build pFUnit
run: |
git clone --depth 1 --branch v4.10.0 https://github.com/Goddard-Fortran-Ecosystem/pFUnit.git
cd pFUnit
pwd
cmake -B./build -S.
cd build
make install

- name: Build atmospheric_physics
run: |
cmake \
-DCMAKE_PREFIX_PATH=/home/runner/work/atmospheric_physics/atmospheric_physics/pFUnit/build/installed \
-DATMOSPHERIC_PHYSICS_ENABLE_CODE_COVERAGE=ON \
-B./build \
-S./test/unit-test
cd build
make

- name: Run tests
run: |
cd build && ctest -V --output-on-failure --output-junit test_results.xml

- name: Upload unit test results
uses: actions/upload-artifact@v4
with:
name: unit-test-results
path: build/test_results.xml

- name: Setup GCov
run: |
python3 -m venv venv
source venv/bin/activate
pip3 install gcovr

- name: Run Gcov
run: |
source venv/bin/activate
cd build
gcovr -r .. --filter '\.\./schemes' --html atmospheric_physics_code_coverage.html --txt

- name: Upload code coverage results
uses: actions/upload-artifact@v4
with:
name: code-coverage-results
path: build/atmospheric_physics_code_coverage.html

39 changes: 39 additions & 0 deletions test/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.17)

project(atmospheric_physics VERSION 0.0.1 LANGUAGES Fortran)

find_package(PFUNIT REQUIRED)

if(NOT ATMOSPHERIC_PHYSICS_IS_TOP_LEVEL)
message(WARNING "atmospheric-physics is not integrated into the CMake build of any top level "
"project yet and this CMake is for testing purposes only. "
"Making a change to this project's CMake will not impact the build of "
"a parent project at this time.")
endif()

option(ATMOSPHERIC_PHYSICS_ENABLE_TESTS "Run pFUnit unit tests" OFF)
option(ATMOSPHERIC_PHYSICS_ENABLE_CODE_COVERAGE "Run code coverage tool" OFF)

if(ATMOSPHERIC_PHYSICS_ENABLE_CODE_COVERAGE)
add_compile_options(-O0 --coverage)
add_link_options(--coverage)
endif()

set(CMAKE_BUILD_TYPE Debug)

set(UTILITIES_SRC
../../schemes/utilities/state_converters.F90
../../schemes/utilities/static_energy.F90
../../schemes/utilities/physics_tendency_updaters.F90
include/ccpp_kinds.F90
)

add_library(utilities ${UTILITIES_SRC})
target_compile_options(utilities PRIVATE -ffree-line-length-none)
target_include_directories(utilities PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

if(ATMOSPHERIC_PHYSICS_ENABLE_TESTS OR ATMOSPHERIC_PHYSICS_ENABLE_CODE_COVERAGE)
enable_testing()
add_subdirectory(tests)
endif()

18 changes: 18 additions & 0 deletions test/unit-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Unit Tests

To add or update unit tests, follow the instructions from the [development guide](https://escomp.github.io/CAM-SIMA-docs/atmospheric_physics/development_workflow/#5-unit-testing). Also, make sure [pFUnit](https://github.com/Goddard-Fortran-Ecosystem/pFUnit) is built and installed following the [build directions](https://github.com/Goddard-Fortran-Ecosystem/pFUnit?tab=readme-ov-file#building-and-installing-pfunit) (see the associated workflow file for a detailed example).

To run the tests, from the root directory of your clone, run:

```bash
$ cmake \
-DCMAKE_PREFIX_PATH=<path_to_pfunit>/build/installed \
-DATMOSPHERIC_PHYSICS_ENABLE_CODE_COVERAGE=ON \
-B./build \
-S./test/unit-test
$ cd build
$ make
$ ctest -V --output-on-failure
```

Where `<path_to_pfunit>` is the path to your pfunit repository. The install path of pFUnit may be different depending on how you've built your local verison.
11 changes: 11 additions & 0 deletions test/unit-test/include/ccpp_kinds.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ccpp_kinds

use ISO_FORTRAN_ENV, only: kind_phys => REAL64

implicit none
private

public :: kind_phys

end module ccpp_kinds

2 changes: 2 additions & 0 deletions test/unit-test/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(utilities)

5 changes: 5 additions & 0 deletions test/unit-test/tests/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_pfunit_ctest(utilities_tests
TEST_SOURCES test_state_converters.pf
LINK_LIBRARIES utilities
)

27 changes: 27 additions & 0 deletions test/unit-test/tests/utilities/test_state_converters.pf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@test
subroutine test_temp_to_potential_temp()
use funit
use state_converters, only : temp_to_potential_temp_run
use ccpp_kinds, only: kind_phys

integer, parameter :: ncol = 5
integer, parameter :: nz = 5

real(kind_phys) :: temp(ncol, nz)
real(kind_phys) :: exner(ncol, nz)
real(kind_phys) :: theta(ncol, nz)
character(len=512) :: errmsg
integer :: errflg

temp = 1
exner = 1
theta = 1

errmsg = ""
errflg = 0

call temp_to_potential_temp_run(ncol, nz, temp, exner, theta, errmsg, errflg)

@assertEqual(0, errflg)

end subroutine test_temp_to_potential_temp