How to write testable classes in WordPress

I’m currently doing some modifications on the WordPress plugin that I created in order to make it testable. The problem is I don’t know which methods am I suppose to write tests for. I have methods which uses some of these functions and global variables:

is_singular()
$_GET
$_POST
$_SESSION

is_singular, is_page, is_home and other functions depends on the context. I’m not sure how to fake those context in a test.

Read More

I tried passing those as arguments to the methods instead but there are some hooks that doesn’t accept additional arguments so I can’t really pass them.

Is it OK to just supply those from the test class? I’m really having difficulty modifying the plugin in such a way that I don’t use those.

Any ideas?

Update

I’m using wordpress-tests and PHP Unit for the unit testing. I’ve done some research on unit testing before I started and there’s a practice that when unit testing global variables should be avoided.

Related posts

1 comment

  1. WordPress is far from having a standard conventions for unit testing extensions. Or for extension structure in general.

    At current time you should either:

    • research how unit testing framework of your choice recommends to work with globals and such for PHP code in general
    • look into WordPress-specific tools for unit testing that start to appear, such as WP_Mock

Comments are closed.