How do I unit test a plugin with forms?
I've written my first serious WordPress plugin. Before I continue development on the plugin any further, I want to start writing tests for it. I've successfully installed phpunit and have got a test environment for the plugin up and running with WP_UnitTestCase
.
The plugin generates a 5 step form meaning the user starts with Step 1, hits submit, goes to Step 2, hits submit (or goes back to step 1), etc. until the form is filled out and spits out an email on the last step.
I can think of two different approaches to this problem: 1) script a browser bot to fill out the form and make sure it works. 2) simulate http requests to post data to the forms and test the output of the html directly.
But I have questions:
If I go the bot approach, is there some kind tool that will help me integrate the bot in with the WordPress testing framework?
How do I make http requests to the WordPress site? It looks like I would use https://developer.wordpress.org/reference/classes/wp_http/. But how do I handle things like session cookies? And what URL do I use to make the request?
Which approach is preferable? HTTP requests or a bot? Does one method offer advantages over the other? I'm inclined to go with HTTP requests as it seems it would be faster and can probably be more easily integrated in with
WP_UnitTestCase.
But I'd like to hear how someone with experience has approached this problem.
Thanks!
Topic unit-tests Wordpress
Category Web