What frameworks and tools would you recommend for unit-testing and mock objects in Perl?
I have an existing Perl application, that mainly does database access, reading and writing files. The application is basically a batch job type of application, it reads in bunch of stuff from files and database and writes a bunch of new files and some stuff to database.
The application currently does not have any unit-test, but I'd like refactor application to have good unit tests.
What frameworks and tools would you recommend for unit-testing and mocking objects? For example something similar to Hamcrest and JMock of Java?
Also, are there any good BDD (Behaviour Driven Development) based testing frameworks for Perl?
JUnit is an open source framework you can use to write and run tests. It aims to help develop bug-free and reliable code written in Java. JUnit provides test runners that run tests and assertions to test the expected results.
We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way to mock an object. We are using JUnit 5 to write test cases in conjunction with Mockito to mock objects.
What is a mocking framework? Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks.
In terms of test frameworks, I like to use Test::Class, which lets you set up nice unit test hierarchies in a manner similar to JUnit. The linked documentation gives a pretty good overview, though if you're not already familiar with Perl testing conventions, check out the documentation for Test::Simple and TAP, which is the standard output format for Perl tests.
For mocking, there is Test::Mock::Class, which is useful for setting up mock classes for libraries outside your direct control, like CPAN modules that your application relies on.
There is also the excellent Test::MockDBI which provides special facilities for mocking your database stuff.
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