Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you write a unit test for a function that is not exported?

I have a module that I'm writing unit tests for to run with travis.ci.

In my module I perform HTTP POST operations to a web service.

One of my internal only functions validate_http_response() is integral to the functions I'm creating to wrap around web service calls, so I'd like to test it. However, because there is no such export validate_http_response the function can't be "seen" by my test script and I get the error:

validate_http_response not defined

How should I structure my test so that I don't have to copy and paste the internal functions into the test itself (there are a few of them)? I'd like to prevent having to maintain a src and test script simultaneously.



EDIT Along with the accepted answer I also found I could do the following at the beginning of the test script: include("../src/myfunctions.jl"), as I have a separate test script for each file in the src.

like image 444
Adrian Torrie Avatar asked Oct 29 '14 10:10

Adrian Torrie


1 Answers

Check out the documentation on modules to better understand how namespacing works. There is no forced visibility in Julia, so you can always access functions, exported or non-exported, in any module by fully qualifying the reference.

So in your case, if your module is named HTTP, you could say HTTP.validate_http_response to access your unexported function to test.

like image 94
quinnj Avatar answered Nov 05 '22 17:11

quinnj