Installing Required Tools
To perform unit testing on Fortran code in Ubuntu, you need a Fortran compiler and a dedicated testing framework. The most commonly used compiler is gfortran (available via Ubuntu’s default repositories), and popular frameworks include pFUnit (a modern, feature-rich option) and FRUIT (a simpler, assertion-based toolkit).
sudo apt update && sudo apt install gfortran to get the latest version of the GNU Fortran compiler. This is essential for compiling both your source code and test cases.sudo apt install pfunit to install pFUnit, which supports object-oriented Fortran and provides advanced features like test fixtures and parallel execution.sudo apt install fruit to install FRUIT, a lightweight framework that uses simple assertion macros for writing tests. FRUIT is ideal for beginners or small projects.Writing Test Cases with FRUIT
FRUIT simplifies unit testing by providing built-in assertion functions and a structured workflow. Below is an example for testing a simple Fortran module that adds two numbers.
Create the Source Module: Save the following code as functions.f90. This module contains the function add that you want to test.
module functions
implicit none
contains
function add(a, b) result(c)
real, intent(in) :: a, b
real :: c
c = a + b
end function add
end module functions
Write the Test Program: Create a file named test_functions.f90. This program uses FRUIT to define test cases and verify the output of add.
program test_functions
use functions
use fruit
implicit none
! Initialize the test suite
call init_unit_tests('Add Function Tests')
! Test case 1: Positive numbers
call assert_equals(5.0, add(2.0, 3.0), '2.0 + 3.0 should equal 5.0')
! Test case 2: Negative numbers
call assert_equals(-2.0, add(-1.0, -1.0), '-1.0 + (-1.0) should equal -2.0')
! Test case 3: Mixed numbers
call assert_equals(0.0, add(1.5, -1.5), '1.5 + (-1.5) should equal 0.0')
! Finalize and run the tests
call finalize_unit_tests()
end program test_functions
Key components:
init_unit_tests: Initializes the test suite with a descriptive name.assert_equals: Compares the actual result (from add) with the expected result. If they don’t match, it prints an error message.finalize_unit_tests: Runs all registered tests and reports the results.Writing Test Cases with pFUnit
pFUnit offers more advanced features (e.g., test fixtures, parallel testing) and integrates better with modern Fortran projects. Here’s how to test the same add function using pFUnit.
test_functions.pf90. pFUnit uses special directives (like @test) to mark test cases.program test_functions
use functions
use pfunit
implicit none
! Mark this program as a test suite
@testSuite
! Test case 1: Positive numbers
@test
subroutine test_add_positive()
real :: result
result = add(2.0, 3.0)
@assertEqual(5.0, result, '2.0 + 3.0 should equal 5.0')
end subroutine test_add_positive
! Test case 2: Negative numbers
@test
subroutine test_add_negative()
real :: result
result = add(-1.0, -1.0)
@assertEqual(-2.0, result, '-1.0 + (-1.0) should equal -2.0')
end subroutine test_add_negative
end program test_functions
Key components:
@testSuite: Marks the program as a test suite.@test: Labels a subroutine as a test case.@assertEqual: Asserts that two values are equal (similar to FRUIT’s assert_equals).Compiling and Running Tests
The compilation process for Fortran unit tests requires linking the source module, the test program, and the testing framework’s libraries.
Compile and Run FRUIT Tests:
Use gfortran to compile the source module and test program together, then run the resulting executable:
gfortran -o test_functions functions.f90 test_functions.f90 -lfruit
./test_functions
Expected output (if all tests pass):
All tests passed!
Compile and Run pFUnit Tests:
pFUnit requires a specific compilation workflow using its pfunit command. First, navigate to the directory containing your test file, then run:
pfunit -c test_functions.pf90
./test_functions
The pfunit command handles linking with the pFUnit library. Expected output (if all tests pass):
Test Suite: Add Function Tests
test_add_positive: PASSED
test_add_negative: PASSED
All tests passed!
Best Practices for Fortran Unit Testing
'2.0 + 3.0 should equal 5.0') to quickly identify failing tests.