pFUnit icon indicating copy to clipboard operation
pFUnit copied to clipboard

Question: Is there a recommended way to test subroutines contained within a subroutine?

Open Roborii99 opened this issue 4 years ago • 1 comments
trafficstars

For instance, If I needed unit test this subroutine:

subroutine pythag(a,b,c)
real :: aa, bb
call square(a,aa)
call square(b,bb)
c = SQRT(aa + bb)
contains
    subroutine square(a,result)
        result = a * a
    end subroutine square
end subroutine pythag

Currently I can't call the square function from my test because it is hidden within the contains. Is there a way around this for unit testing purposes?

Roborii99 avatar Jun 23 '21 18:06 Roborii99

No, or at least not as written. There is an inherent conflict between privacy/encapsulation and testability. A test must be able to invoke the logic to be tested. So here you can test pythag() but not square.

One approach that I use that is not quite directly applicable: When I have a procedure I want to test that I don't want to make public, I divide my module into 2 modules. The first module, which I usually give a _private suffix to its name, makes all of the procedures public. The second module USEs the first module but only makes a few of those procedures PUBLIC. Users are expected to USE the second module but my tests USE the first module. But even that won't work for you unless you are willing to move square() such that it is not an internal procedure.

tclune avatar Jun 24 '21 00:06 tclune