Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of...

98
Parallel Fortran Unit Testing Framework Installation, Usage, and API Tom Clune Advanced Software Technology Group Computational and Information Sciences and Technology Office NASA Goddard Space Flight Center April 10, 2014 Tom Clune (ASTG) pFUnit - NCAR April 10, 2014 1 / 117

Transcript of Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of...

Page 1: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Parallel Fortran Unit Testing FrameworkInstallation, Usage, and API

Tom Clune

Advanced Software Technology GroupComputational and Information Sciences and Technology Office

NASA Goddard Space Flight Center

April 10, 2014

Tom Clune (ASTG) pFUnit - NCAR April 10, 2014 1 / 117

Page 2: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 IntroductionOverviewQuick review of testing

2 Introduction to pFUnit

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 2 / 117

Page 3: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 IntroductionOverviewQuick review of testing

2 Introduction to pFUnit

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 3 / 117

Page 4: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Class Overview

Primary Goals

Learn how to use pFUnit 3.0 to create and run unit-tests

Learn how to apply test-driven development

Gain greater appreciation for software testing

Prerequisites

Familiarity with F95 syntax

Familiarity with MPI1

Access to Fortran compiler supported by pFUnit 3.0

Possibly beneficial skills

Various F2003 syntax

Object-oriented programming

CMake

1Only for MPI-specific sections.Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 4 / 117

Page 5: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Syllabus

Thursday PM - Introduction to pFUnitI Overview of pFUnit and unit testingI Build and install pFUnitI API for pFUnitI The pFUnit preprocessor

Friday AM - Advanced topics (including TDD)I User-defined test subclassesI Parameterized testsI Introduction to TDDI Examples and exercises usind TDD and pFUnit

Friday PM - Bring-your-own-codeI Introduce pFUnit into the build process for your own codeI Apply TDD within your own codeI Supplementray exrecises will be available

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 5 / 117

Page 6: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Materials

These slides can be downloaded at ...

The exercises can be downloaded at ...

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 6 / 117

Page 7: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 IntroductionOverviewQuick review of testing

2 Introduction to pFUnit

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 7 / 117

Page 8: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Quick review of testing

What is a (software) test?

What are the different types of software tests?

What are desirable properties for unit tests?

What is the anatomy of a unit test?

What is a test “fixture”

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 8 / 117

Page 9: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

A test by any other name ...

A test is any mechanism that can be used to verify a softwareimplementation. Examples include:

Conditional termination during execution:

IF (PA(I,J)+PTOP.GT .1200.) &

call stop_model(’ADVECM: Pressure diagnostic error ’,11)

Diagnostic print statement

print*, ’loss of mass = ’, deltaMass

Inspection of rendered output:Temp1

5 10 15

10

20

30

40

50

60

Temp2

5 10 15

10

20

30

40

50

60

Difference

5 10 15

10

20

30

40

50

60

Student Version of MATLAB

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 10 / 117

Page 10: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Taxonomy of Testing

ScopeI UnitI IntegrationI System

Acceptance

Stress

Performance

Regression

AccessI Black boxI White box

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 11 / 117

Page 11: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:

Narrow/specific

I Failure of a test localizes defect to small section of code.

Orthogonal to other tests

I Each defect causes failure in one or only a few tests.

Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effects

I No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

Frugal

I Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatableClear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 12: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:Narrow/specific

I Failure of a test localizes defect to small section of code.

Orthogonal to other tests

I Each defect causes failure in one or only a few tests.

Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effects

I No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

Frugal

I Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatableClear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 13: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:Narrow/specific

I Failure of a test localizes defect to small section of code.Orthogonal to other tests

I Each defect causes failure in one or only a few tests.

Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effects

I No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

Frugal

I Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatableClear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 14: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:Narrow/specific

I Failure of a test localizes defect to small section of code.Orthogonal to other tests

I Each defect causes failure in one or only a few tests.Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effects

I No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

Frugal

I Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatableClear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 15: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:Narrow/specific

I Failure of a test localizes defect to small section of code.Orthogonal to other tests

I Each defect causes failure in one or only a few tests.Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effectsI No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

Frugal

I Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatableClear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 16: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:Narrow/specific

I Failure of a test localizes defect to small section of code.Orthogonal to other tests

I Each defect causes failure in one or only a few tests.Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effectsI No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

FrugalI Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatableClear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 17: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:Narrow/specific

I Failure of a test localizes defect to small section of code.Orthogonal to other tests

I Each defect causes failure in one or only a few tests.Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effectsI No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

FrugalI Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatable

Clear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 18: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Desirable attributes for tests:Narrow/specific

I Failure of a test localizes defect to small section of code.Orthogonal to other tests

I Each defect causes failure in one or only a few tests.Complete

I All functionality is covered by at least one test.I Any defect is detectable.

Independent - No side effectsI No STDOUT; temp files deleted; ...I Order of tests has no consequence.I Failing test does not terminate execution.

FrugalI Execute quickly (think 1 millisecond)I Small memory, etc.

Automated and repeatableClear intent

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 12 / 117

Page 19: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)call assertEqual(9., trajectory(2.,3.))@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 20: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)call assertEqual(9., trajectory(2.,3.))@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 21: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)call assertEqual(9., trajectory(2.,3.))@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 22: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)call assertEqual(9., trajectory(2.,3.))@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 23: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)

call assertEqual(9., trajectory(2.,3.))@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 24: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)

call assertEqual(9., trajectory(2.,3.))@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 25: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)

call assertEqual(9., trajectory(2.,3.))

@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 26: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Anatomy of a Software Test Procedure

testTrajectory() ! s = 12 at2

a = 2.; t = 3.

s = trajectory(a, t)

call assertEqual(9., s)call assertEqual(9., trajectory(2.,3.))

@assertEqual(9., trajectory(2.,3.))(automatically includes

file name and line number)

! no op

Tom Clune (ASTG) pFUnit 3.0 - Introduction - NCAR April 10, 2014 14 / 117

Page 27: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 15 / 117

Page 28: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 16 / 117

Page 29: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Noteworthy features of pFUnit 3.0

2005 Prototype2

read a book and re-implemted with TDD

2006 Version 1.0 released as open source

2012 Began serious attempt at F2003 implementation

2013 Version 2.0 released - heavy reliance of OO

new and improved preprocessor (test “annotations”)additional assertions

2013 Version 2.1 released - listened to user feedback

2014 Version 3.0 released3

introduced cmaketest extensions finally “easy”

2Proof to colleague that Fortran (F90) - I cheated3Would have been 2.1, but bug in gfortran broke backwards compatibility

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 17 / 117

Page 30: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Noteworthy features of pFUnit 3.0

Standard Fortran4

Strong support for multidimensional arrays

Testing of parallel procedures - MPI & OpenMP5

Test fixtures

Parameterized tests

User-defined extensions (OO)

Test annotations (via preprocessor) for greatly improved ease of use

Improved (and maintained) examples

Extensive regression testing with each push

4F2003 with a dash of F20085Threadsafe

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 18 / 117

Page 31: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Useful resources

Website/documentation http://pfunit.sourceforge.net

Mailing list: [email protected]

This tutoria

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 19 / 117

Page 32: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 20 / 117

Page 33: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Supported Configurations1,2

OS Vendor Version

Linux Intel ifort 14.0.2Linux Intel ifort 13.1.192Linux GNU gfortran 4.8.33

Linux GNU gfortran 4.9.03

Linux NAG nagfor 5.3.2(981)OS X Intel ifort 14.0.2OS X GNU gfortran 4.8.33

OS X GNU gfortran 4.9.03

OS X NAG nagfor 5.3.2(979)

Externally contributed

AIX IBM xlf ???Windows Intel ifort ???

1In many cases closely related compiler versions will also work.2We are cautiously optimistic that PGI will soon be supported.3Not yet released. 4.9.0 experimental currently works. Older versions contained an

insurmountable bug.Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 22 / 117

Page 34: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Obtaining pFUnit

For this discussion we will refer to 3 distinct directories:

root - top directory of downloaded code

build - directory in which build instructions are issued

install - directory where various framework elements will be installedfor later use.

There are 2 ways to obtain the source code for pFUnit:

Via git (read-only):% git clone git://git.code.sf.net/p/pfunit/code pFUnit

Via tar:http://sourceforge.net/projects/pfunit/files/latest/download

% tar zxf ./pFUnit.tar.gz

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 24 / 117

Page 35: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

What’s inside?

./source

./tests

./Examples

./documentation

./include

./bin

README-INSTALL

CMakeLists.txt

GNUmakefile

COPYRIGHT

LICENSE

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 25 / 117

Page 36: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Building pFUnit

1 Set PFUNIT environment variable to desired installation path

bash: % export PFUNIT=<path>

csh: % setenv PFUNIT <path>

You may want to edit your various login scripts to automatically setthis variable.

2 Decide whether to use CMake (recommended) or just GNU make3 Choose configuration options

I Build with MPI supportI Build with OpenMP support

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 27 / 117

Page 37: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

CMake build instructions

1 % cd <builddir>

2 % cmake <rootdir> <options>

Options:

I -DMPI=YES (include support for MPI)I -DOPENMP=YES (include support for OpenMP)

3 % make tests

4 % make install

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 29 / 117

Page 38: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

GNU make build instructions

1 % cd <rootdir>

2 % make tests <options>I MPI=YES (include support for MPI)I OPENMP=YES (include support for OpenMP)I F90_VENDOR=<vendor> (override default vendor: Intel, GNU, NAG)

3 % make install INSTALL_DIR=$PFUNIT

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 31 / 117

Page 39: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Exercise 0: Build and Install pFUnit in your environment

1 Download pFUnit 3.02 Build serial configuration

1 Set $PFUNIT2 Build with cmake/gmake3 Install

3 Build MPI configuration (unless skipping MPI exercises)1 Set $PFUNIT (use different path than for serial)2 Build with cmake/gmake - use MPI option3 Install

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 33 / 117

Page 40: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 34 / 117

Page 41: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple Example: testing factorial function

file: Exercises/SimpleTest/testFactorial.pf

@test

subroutine testFactorialA ()

use pFUnit_mod

use Factorial_mod

@assertEqual (6, factorial (3))

end subroutine testFactorialA

Things to notice:

Test is preceded by @test

Expected results are indicated with @assertEqual

One must “use” the pFUnit_mod module

The “.pf” suffix is an arbitrary convention

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 36 / 117

Page 42: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple Example: testing factorial function

file: Exercises/SimpleTest/testFactorial.pf

@test

subroutine testFactorialA ()

use pFUnit_mod

use Factorial_mod

@assertEqual (6, factorial (3))

end subroutine testFactorialA

Things to notice:

Test is preceded by @test

Expected results are indicated with @assertEqual

One must “use” the pFUnit_mod module

The “.pf” suffix is an arbitrary convention

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 36 / 117

Page 43: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple Example: testing factorial function (contd́)

file: Exercises/SimpleTest/testSuites.inc

! Register your test suites here

ADD_TEST_SUITE(testFactorial_suite)

This file is included by the driver

Used to register all your tests

One entry per suite; one suite per file

Default test suite name is derived from the test file name

“testSuites.inc” is mandatory – must be in the include path

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 38 / 117

Page 44: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple Example: testing factorial function (contd́)

file: Exercises/SimpleTest/testSuites.inc

! Register your test suites here

ADD_TEST_SUITE(testFactorial_suite)

This file is included by the driver

Used to register all your tests

One entry per suite; one suite per file

Default test suite name is derived from the test file name

“testSuites.inc” is mandatory – must be in the include path

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 38 / 117

Page 45: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Parser Basic Syntax

Assert that two items are equal:

@assertEqual(a, b)

Assert that two items are equal to some tolerance:

@assertEqual(a, b, tolerance)

Register a test procedure

@test

subroutine testProc ()

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 40 / 117

Page 46: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

pFUnit Driver

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 41 / 117

Page 47: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Exercise 1: Simple unit tests

1 Build the “SimpleTest” example in the distribution1 Change directory to ./Exercises/SimpleTest2 % make tests3 Verify that 1 test ran successfully.

2 Add a new test in the file./Exercises/SimpleTest/testFactorial.pf’

1 Create a new test procedure that verifies 5! = 1202 % make tests3 Create a new test procedure that verifies 0! = 14 % make tests (uh oh!)5 Fix the implementation6 % make tests (whew!)

3 Demonstrate tests as a harness1 Edit factorial.F902 Insert a bug (e.g., change ’*’ to ’+’)3 % make tests

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 43 / 117

Page 48: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 44 / 117

Page 49: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple MPI Example: matrix transpose

file: Exercises/SimpleMpiTest/TestTranspose.pf

module TestTranspose_mod

use pFUnit_mod

use Transpose_mod

implicit none

contains

@test(npes =[1])

subroutine testTranspose_1by1(this)

class (MpiTestMethod), intent(inout) :: this

real :: a(1,1)

real :: at(1,1)

integer :: comm

! preconditions

a = 1

at = 0

comm = this%getMpiCommunicator ()

call transpose(comm , a, at)

@mpiAssertEqual (1, at)

end subroutine testTranspose_1by1

end module TestTranspose_mod

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 46 / 117

Page 50: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple Mpi Tests

Declare with @test(npes=[<integer_list>])

Framework runs test once for each value of npes

Framework creates a new subcommunicator each time test is run

Framework passes communicator through a mandatory argument

declare as class(MpiTestMethod), intent(inout) :: <arg>6

Passed object has several useful methods:

comm = this%getMpiCommunicator ()

npes = this%getNumProcesses ()

rank = this%getProcessRank ()

Any assertions that fail will attach information aboutI npes for failing caseI rank for failing case

6The intent must be inout.Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 48 / 117

Page 51: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Exercise 2: Extend transpose tests

1 Set $PFUNIT to the MPI build2 Build the “SimpleMpiTest” example in the distribution

1 Change directory to ./Exercises/SimpleMpiTest2 % make tests3 Verify that 2 tests ran successfully.

3 Create a new test that works for multiple values of npes.1 Create a helper function that specifies

aij = aTji = f (i , j) = np ∗ i + j

2 Make arrays a and aT (expected and found) allocatable3 Fill a and expected aT using helper function4 Call transpose5 Assert that the found value is the same as the expected value.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 50 / 117

Page 52: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 51 / 117

Page 53: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Test FixturesA test fixture is any mechanism that allows a consistent initialization fortest preconditions.

Group of tests have same initial conditionsComplex sequence of steps to create preconditionsEnsures release of system resources (memory, files, ...)

Testing frameworks generally provide mechanisms to encapsulate the logicfor test preconditions and cleanup. Usually this is in the form ofprocedures named setUp() and tearDown().

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 52 / 117

Page 54: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple fixture (unencapsulated)

Can be expedient to use a global variable or a persistent file as aquick-and-dirty fixture:module SimpleFixture_mod

use pFunit

use Reader_mod

contains

@before

subroutine init()

open(10,file=’tmp.dat ’,status=’new ’)

write (10) 1

write (10) 2

close (10)

end subroutine init

@after

subroutine done()

open(10,file=’tmp.dat ’,status=’unknown ’)

close(10, status=’delete ’)

end subroutine done

...

end module SimpleFixture_modTom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 54 / 117

Page 55: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Simple fixture (contd́)

@before indicates procedure to run before each test in fileI Convention is to call procedure setUp()

@after indicates procedure to run after each test in fileI Convention is to call procedure tearDown()

Because no arguments are passed to procedures, fixture data must bein the form of global variables (module, common) or the file system(persistent file)

I Dangerously close to violating rule: “No Side Effects!”

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 56 / 117

Page 56: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 57 / 117

Page 57: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 58 / 117

Page 58: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Exception class

Role: Used to notify/detect “undesired” states during execution. Limitedemulation of exceptions provided by other high-level languages (C++,Java, Python, etc).Implementation:

Manages a global, private stack of Exception objects.

Each Exception object has a message, and a location (file+line).

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 60 / 117

Page 59: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Exception API

subroutine throw(message[, location ])

subroutine catch(

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 62 / 117

Page 60: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Assert modules

Role: Used to express intended/expected relationships among variables.Implementation:

Heavily overloaded suite of procedures with consistent style forinterface.

When the intended relationship does not hold, the layer pushes aself-explanatory Exception onto the global exception stack.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 63 / 117

Page 61: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Logical Assertions

call assertTrue(condition)

call assertFalse(condition)

call assertAny(conditions)

call assertAll(conditions)

call assertNone(conditions)

call assertNotAll(conditions)

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 65 / 117

Page 62: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

String Assertions

call assertEqual(expected , found)

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 67 / 117

Page 63: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Integer Assertions

call assertEqual(expected , found)

Overloaded for up to rank 27

Only supports default KIND

The following are only supported for scalars:

call assertLessThan(a, b) ! a < b

call assertLessThanOrEqual(a, b) ! a <= b

call assertGreaterThan(a, b) ! a > b

call assertGreaterThanOrEqual(a, b) ! a >= b

7Use a support request if you need more.Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 69 / 117

Page 64: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

String Assertions

call assertEqual(expected , found)

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 71 / 117

Page 65: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

API - AssertEqual (Real)

Compare two values and throw exception if different

|a− b| <= δ

call assertEqual(expected , found[, tolerance ])

Uses absolute error (as opposed to relative error)Overloaded for multiple KINDs (32 and 64 bit)Overlaoded for multiple ranks (up through 5D)Optional tolerance – default is exact equalityUses L∞ normTo reduce exponential number of overloads:

I KIND(expected) <= KIND(found)I KIND(tolerance) == KIND(found)I RANK(expected) == RANK(found) or scalar

Example message:

expected: +1.000000 but found: +3.000000;

difference: |+2.000000| > tolerance :+0.000000.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 73 / 117

Page 66: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

API - Assert variants (Real)

call assertLessThan(expected , found)

call assertGreaterThan(expected , found)

call assertLessThanOrEqual(expected , found)

call assertGreaterThanOrEqual(expected , found)

If relative tolerance is desired:

|a− b||a|

<= δ

call assertRelativelyEqual(expected , found[, tolerance ])

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 75 / 117

Page 67: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

API - AssertEqual (Complex)

Compare two values and throw exception if different

|a− b| <= δ

call assertEqual(expected , found[, tolerance ])

Overloaded for multiple KINDs (32 and 64 bit)

Overlaoded for multiple ranks (up through 5D)

Optional tolerance – default is exact equality

To reduce exponential number of overloads:I KIND(expected) <= KIND(found)I KIND(tolerance) == KIND(found)I RANK(expected) == RANK(found) or scalar

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 77 / 117

Page 68: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Miscellaneous other Assert procedures

call assertIsNaN(x) ! single/double

call assertIsFinite(x) ! single/double

call assertExceptionRaised ()

call assertExceptionRaised(message)

call assertSameShape(expectedShape , foundShape)

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 79 / 117

Page 69: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Hierarchy of Test Classes

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 80 / 117

Page 70: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Test

Role: Abstract base class for all test objects.Implementation: Framework provides various subclasses forcommon/generic cases. Users can define custom subclasses for specificpurposes. Provided subclasses include:

TestCase

TestMethod

MpiTestCase

MpiTestMethod

TestSuite

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 81 / 117

Page 71: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

TestSuite

Role: Aggregates collection of tests into single entity.Implementation: TestSuite objects are simultaneously Test objects andcollections of tests. Run() method applies run() to each contained test.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 82 / 117

Page 72: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

TestCase class

Role: Abstract Test subclass that provides some services that are commonto most Test subclasses.Implementation:

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 83 / 117

Page 73: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

TestMethod class

Role: Simple concrete Test subclass that supports the common casewhere test procedure receives no arguments.Implementation: Constructor stores a procedure pointer to vanillaFortran subroutine with no arguments. A restricted form of test fixture ispermitted by specifying setUp() and tearDown() methods that also haveno arguments. (I.e. fixture is not encapsulated.)

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 84 / 117

Page 74: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

TestMethod API

Constructor:

function TestMethod(name , method[, setUp , tearDown ])

character(len=*), intent(in) :: name

procedure(empty) :: method

procedure(empty) :: setUp

procedure(empty) :: tearDown

Methods:

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 86 / 117

Page 75: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

ParameterizedTestCase class

Role: Allows a single test procedure to be execute multiple times withdifferent input values.Implementation: ParameterizedTestCase objects contain anAbstractTestParameter object that encapsulates input. Subclasses ofParameterizedTestCase must generally also subclassAbstractTestParameter.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 87 / 117

Page 76: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

MpiTestCase class

Role: (Abstract) Extends ParameterizedTestCase with support for MPI.Implementation: MpiTestCase modifies the runBare() launch mechanismto create an appropriately sized MPI group and correspondingsubcommunicator. Processes within that group then call the user’s testprocedure, while any remaining processes wait at a barrier.MPI based tests must not use MPI_COMM_WORLD, and must instead obtainMPI context from the passed test object.The following convenient type-bound procedures are provided:

getProcessRank () ! returns rank within group

getNumProcesses () ! returns size of group

getMpiCommunicator () ! returns the bare MPI communicator

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 89 / 117

Page 77: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

MpiTestMethod class

Role: Simple concrete Test subclass that supports common MPI casesthat just need basic MPI context.Implementation: Analogous to the vanilla TestMethod, except that usertest procedures are now passed an object which must be queried for anyMPI context that the test needs.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 90 / 117

Page 78: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

MpiTestMethod API

Constructor:

function MpiTestMethod(name , method , numProcesses , [, setUp , tearDown ])

character(len=*), intent(in) :: name

procedure(empty) :: method

integer :: numProcesses ! requested

procedure(empty) :: setUp

procedure(empty) :: tearDown

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 92 / 117

Page 79: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

TestResult class

Role: “Scorecard” – accumulates information about tests as they run.Implementation: Each run() method for Test objects has a mandatoryTestResult argument. The Visitor pattern is used to allow the TestResultobject to manage and monitor the test as it progresses.Note: Visitor is a somewhat advanced pattern and uses OO capabilities ina nontrivial manner. Users should not need to be aware of this, butdevelopers of framework extensions likely will.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 93 / 117

Page 80: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Abstract BaseTestRunner class

Role: Runs a test (usually a TestSuite).Implementation: Run() method constructs and configures a TestResultobject, then runs the passed Test object.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 94 / 117

Page 81: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

TestRunner class

Role: Default Runner for pFUnit.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 95 / 117

Page 82: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

RobustRunner class

Role: Runner subclass that executes tests within a separate process.Implementation: Collaborates with SubsetRunner. RobustRunner restartsSubsetRunner if it detects a hang or a crash. Currently a bit unreliable.(Irony)

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 96 / 117

Page 83: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnitpFUnit overviewBuild and InstallBasic pFUnit ExampleSimple MPISimple Fixtures (unencapsulated)Testing for error handlingAPIParser and Driver

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 97 / 117

Page 84: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Annotations: Miscellaenous

@suite

Overrides default name for generated function which constructs testsuite for the input file. Default is <base>_suite for file with externaltest procedures, and <module_name>_suite for files that contain amodule.

@before

Indicates next line begins a setUp() procedure for subsequent testprocedures.

@after

Indicates next line begins a tearDown() procedure for subsequent testprocedures.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 99 / 117

Page 85: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Annotations: @assert*

@assert *(...)

1 Calls corresponding Fortran assert procedure

2 Inserts argument for file & line number

3 Inserts conditional return if exception is thrown

For example, if line 100 of file ’myTests.pf’ is:

@assertEqual(x, y, tolerance)

Expands to

!@assertEqual(x, y, tolerance)

call assertEqual(x, y, tolerance , &

& SourceLocation(’myTests.pf’, 100))

if (anyExceptions ()) return

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 101 / 117

Page 86: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Annotations @test

@test

@test(<options >)

Indicates that next line begins a new test procedure

Appends test procedure in the file’s TestSuite

Accepts the following options:I ifdef=<token> Enables conditional compilation of testI npes=[<list-of-integers>] Specifies that test is to run in a

parallel context on the given numbers of processes.I esParameters={expr} Run this test once for each value in expr. Expr

can be an explicit array of TestParameter’s or a function that returnssech an array.

I cases=[<list-of-integers>] Alternative mechanism for specifyingtest parameters where a single integer is passed to the test constructor.

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 103 / 117

Page 87: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Annotations: @testCase

@testCase

@testCase(<options >)

Indicates next line defines a new derived type which extends TestCase.All test procedures in file must accept a single argument of thatextended type.Accepts the following options:

I constructor=<name> Specifies the name of the function to constructcorresponding test object. Default is a constructor with same name asderived type8

I npes=[<list-of-integers>] Indicates that extension is a subclassof MpiTestCase, and provides a default set of values for NPES for alltest procedures in the file. Individual tests can override.

I esParameters={expr} Indicates that extension is a subclass ofParameterizedTestCase, and provides a default set of parameters for alltests in the file. Can be overridden by each test.

I cases=[<list-of-integers>] Alternative mechanism for specifyingdefault test parameters where a single integer is passed to the testconstructor.

8This F2003 feature is somewhat unreliable - esp. prior to 14.0.2.Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 105 / 117

Page 88: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Annotations: @testParameter

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 107 / 117

Page 89: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

pFUnit driver

Tom Clune (ASTG) pFUnit 3.0 - Introduction to pFUnit - NCAR April 10, 2014 108 / 117

Page 90: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnit

3 Advanced pFUnitOO Fortran - what you need to knowpFUnit test HierarchyFixtures (encapsulated)Parameterized TestsTest Listeners and Runners

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Advanced pFUnit - NCAR April 10, 2014 109 / 117

Page 91: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnit

3 Advanced pFUnitOO Fortran - what you need to knowpFUnit test HierarchyFixtures (encapsulated)Parameterized TestsTest Listeners and Runners

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Advanced pFUnit - NCAR April 10, 2014 110 / 117

Page 92: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnit

3 Advanced pFUnitOO Fortran - what you need to knowpFUnit test HierarchyFixtures (encapsulated)Parameterized TestsTest Listeners and Runners

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Advanced pFUnit - NCAR April 10, 2014 111 / 117

Page 93: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnit

3 Advanced pFUnitOO Fortran - what you need to knowpFUnit test HierarchyFixtures (encapsulated)Parameterized TestsTest Listeners and Runners

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Advanced pFUnit - NCAR April 10, 2014 112 / 117

Page 94: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnit

3 Advanced pFUnitOO Fortran - what you need to knowpFUnit test HierarchyFixtures (encapsulated)Parameterized TestsTest Listeners and Runners

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Advanced pFUnit - NCAR April 10, 2014 113 / 117

Page 95: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnit

3 Advanced pFUnitOO Fortran - what you need to knowpFUnit test HierarchyFixtures (encapsulated)Parameterized TestsTest Listeners and Runners

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Advanced pFUnit - NCAR April 10, 2014 114 / 117

Page 96: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Outline

1 Introduction

2 Introduction to pFUnit

3 Advanced pFUnit

4 Test-driven development

Tom Clune (ASTG) pFUnit 3.0 - Test-driven development - NCAR April 10, 2014 115 / 117

Page 97: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

References

pFUnit: http://sourceforge.net/projects/pfunit/

Tutorial materialsI https://modelingguru.nasa.gov/docs/DOC-1982I https://modelingguru.nasa.gov/docs/DOC-1983I https://modelingguru.nasa.gov/docs/DOC-1984

TDD Bloghttps://modelingguru.nasa.gov/blogs/modelingwithtdd

Test-Driven Development: By Example - Kent Beck

Mller and Padberg,”About the Return on Investment of Test-DrivenDevelopment,” http://www.ipd.uka.de/mitarbeiter/muellerm/

publications/edser03.pdf

Refactoring: Improving the Design of Existing Code - Martin Fowler

JUnit http://junit.sourceforge.net/

Tom Clune (ASTG) pFUnit 3.0 - Test-driven development - NCAR April 10, 2014 116 / 117

Page 98: Parallel Fortran Unit Testing Framework · 2020-05-12 · 1 Introduction Overview Quick review of testing 2 Introduction to pFUnit 3 Advanced pFUnit 4 Test-driven development Tom

Acknowledgements

This work has been supported by NASA’s High End Computing(HEC) program and Modeling, Analysis, and Prediction Program.

Many thanks to team members Carlos Cruz and Mike Rilee forhelping with implementation, regression testing and documentation.

Special thanks to members of the user community that have madecontributions.

Tom Clune (ASTG) pFUnit 3.0 - Test-driven development - NCAR April 10, 2014 117 / 117