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

I have a module in which I write unit tests for working with travis.ci.

In my module, I perform HTTP POST operations for the web service.

One of my internal functions, validate_http_response() is an integral part of the functions that I create to exchange web service calls, so I would like to test it. However, since there is no such export validate_http_response , the function cannot be "seen" by my test script, and I get an error:

 validate_http_response not defined 

How should I structure my test so that I do not have to copy and paste the internal functions into the test itself (there are several of them)? I would like to prevent src and test script from being saved at the same time.



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

+6
source share
2 answers

Check out the module documentation for a better understanding of how the namespace works. There is no forced visibility in Julia, so you can always access functions exported or not exported in any module by fully specifying a link.

So in your case, if your module is called HTTP , you can say HTTP.validate_http_response to access your unexported function for testing.

+9
source

There are two solutions:

  • Export function.
  • Create a new module containing the HTTP request validation code. Move the function there. Now it is part of the official / public API and can be tested independently.

The first solution is simple, but supports your API. The second is pure, but probably most of the work.

0
source

Source: https://habr.com/ru/post/1205770/


All Articles