I'm writing a perl module that interfaces with an API and I would like to write a test suite for it before putting it up on CPAN. However, because this module is basically just an interface for an API, all tests would require a valid API key and user. Obviously I can't release this module with my API key and username in the test suite, so what is the best way to handle something like this? Should I just test locally and then put it up on CPAN with no tests? Has anyone run into this before and come up with a good solution? I know that writing tests is best practice, so I'd like to do it if I can. Thanks!
Why not wrap the API calls into small functions (e.g. the function does NOTHING but the API call), and then mock those functions in your tests as needed using Test::MockObject
or similar?
This would be even better, as you would be able to have tests that test different results from API (failure, auth failure, etc...)
I made it clear in my documentation that my module was useless without an API key, and used skip:{} constructs of Test::More to skip all the tests if the key was not present. You can choose too bail_out instead of skip.
Just insure that your docs explain how to communicate the API key to the module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With