Ubuntu如何进行Fortran单元测试
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).
- Install gfortran: Run
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. - Install pFUnit: Use the command
sudo apt install pfunit
to install pFUnit, which supports object-oriented Fortran and provides advanced features like test fixtures and parallel execution. - Install FRUIT: Run
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 functionadd
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 ofadd
.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 (fromadd
) 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.
- Create the Test Program: Save the following code as
test_functions.pf90
. pFUnit uses special directives (like@test
) to mark test cases.
Key components: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
@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’sassert_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:
Usegfortran
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 itspfunit
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
- Cover All Edge Cases: Test not just typical inputs (e.g., positive numbers) but also edge cases (e.g., zero, very large/small numbers, empty inputs).
- Use Descriptive Messages: Write clear error messages in assertions (e.g.,
'2.0 + 3.0 should equal 5.0'
) to quickly identify failing tests. - Automate Testing: Integrate your tests into a continuous integration (CI) system (e.g., GitHub Actions, GitLab CI) to run them automatically on every code change.
- Refactor Tests: Keep test code modular and reusable (e.g., extract common setup code into subroutines) to reduce duplication.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu如何进行Fortran单元测试
本文地址: https://pptw.com/jishu/727687.html