Questions tagged [unit-tests]

Automated tests for the smallest parts of code.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
25 views

How to avoid, skip or fix "Uncaught Return type of Requests_Cookie_Jar::offsetExists($key)" during phpunit

Phpunit 9.5.21, Ubuntu 22.04, PHP 8.1.2 I can not unit test my plugin locally on Ubuntu anymore because the shipped php version is 8.1, and it fails. in /home/username/dev/wptests/wp/wp-includes/...
user avatar
0 votes
0 answers
22 views

no_rest_route in Unit Testing for REST endpoint for CPT registered via show_in_rest

I've created a REST route for custom post type 'book' by passing 'show_in_rest' => true, as args in register_post_type('books', $args); So, when I enter in URL https://localhost/wordpress/wp-json/...
user avatar
  • 71
0 votes
0 answers
13 views

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 ...
user avatar
  • 167
1 vote
1 answer
70 views

PHPUnit Plugin Integration Tests: Getting wordpress-tests-lib PHP into IDE (VS Code)

So I've managed to follow the instructions to get the "Plugin Integration Tests" set up using wp-cli and the install-wp-tests.sh script as described in the handbook article: https://make....
user avatar
  • 2,903
2 votes
0 answers
253 views

Testing custom API endpoint with class dependency

Sorry I feel like really stuck here. I have a plugin introducing a new Rest API controller (WP_REST_Controller) with basically a single endpoint which uses a separate class as a client to fetch some ...
user avatar
0 votes
1 answer
131 views

Mocking WP_CLI static methods in unit tests

I'm writing unit tests (not integration) for a lib I'm working on. There, we created some WP-CLI commands that I'd like to test. Most of them are just renaming things, copying and pasting things over ...
user avatar
  • 1,954
0 votes
1 answer
41 views

How can I enable IDE integration (autocomplete) for WordPress test suite

When testing a theme or plugin, you can use WP-CLI to scaffold the test suite setup, wp scaffold plugin-test plugin. So is there a good way to integrate an editor/IDE (VS Code in my case) so that ...
user avatar
  • 142
2 votes
1 answer
156 views

How to verify meta box is registered in Unit Testing?

I am currently doing TDD with WordPress, in the PHPUnit I need to test if the meta box is actually registered on a method call, I could not find any function in WordPress to do that, so I was trying ...
user avatar
  • 129
1 vote
1 answer
321 views

How do I phpunit test a post output process?

I like to know how to test the html output of a post. Like WP would output it on the frontend in reality. My specific case at this point is to test oembed caching as I want to test re-caching. AFAIK ...
user avatar
0 votes
1 answer
286 views

Why is one phpunit test throwing an error on one class when all other classes are similar without error?

I'm working on the Liquid Messages plugin. There are some basic PHPUnit tests existing for it and when I run phpunit all pass except for one. It errors out: There was 1 error: 1) GCS_Async_Test::...
user avatar
0 votes
1 answer
302 views

PHPUnit Testing and woocommerce Constant

I tried to perform a test on the WC_VERSION constant. The main class : class WC_Custom_Variable_Products_Dependencies { /** minimum WooCommerce version required by this plugin */ const ...
user avatar
  • 162
0 votes
1 answer
267 views

Unit Testing action hook

I want to perform unit tests on a Class, my goal is: I want to check if the plugin is activated or not by using the function: is_plugin_active class WC_Custom_Variable_Products_Dependencies { ...
user avatar
  • 162
1 vote
0 answers
298 views

Coverage in integration tests

EDIT: I've created the mwe as a small test plugin: https://github.com/dingo-d/test-plugin Original question: I'm writing tests for my plugin and one thing makes no sense to me and I'm not sure if I'm ...
user avatar
  • 1,954
1 vote
2 answers
1k views

Unit Test in Wordpress

I work in wordpress with a lot of classes that use wordpress functions and I'd like make unit tests for them. The problem I found is that phpunit doesn't recognize wordpress functions and the tests ...
user avatar
  • 13
0 votes
1 answer
299 views

Wordpress and Wordpress-test-lib not getting added to tmp folder

I’ve installed the wpcli and php unit testing to my imac and the wordpress and wordpress-test-lib files were added to my tmp folder as they should have. I used this to install the setup. bash bin/...
user avatar
1 vote
0 answers
34 views

Underscores Theme Unit Testing - Catching Untitled Posts

Wordpress 5.0.3 | Underscores Theme I'm unit testing a theme I based on underscores using the wptest.io xml. Underscores does not gracefully catch untitled posts. There is no title in either ...
user avatar
  • 121
0 votes
0 answers
157 views

Integration test - remove the mu plugins in the test

As a recurring theme these past few questions I asked, I'm doing integration testing and want to check the activate static method that gets called in my register_activation_hook in the main plugin ...
user avatar
  • 1,954
1 vote
0 answers
72 views

Am I testing this right?

Since I need to have 100% test coverage for the client, I'm writing integration and unit tests in my plugin. And I have a method in a class that is hooked to admin_menu hook, which adds submenu. ...
user avatar
  • 1,954
1 vote
1 answer
204 views

Extending WP_UnitTestCase without any Tests

I want to add some logging to an extension of WP_UnitTestCase. Like this: class MZMBO_UnitTestCase extends WP_UnitTestCase { public function el($message){ file_put_contents('./log_'.date(...
user avatar
  • 529
4 votes
0 answers
46 views

What is the best way to do unit tests for your plugin in 2018 [closed]

I´m making a wordpress plugin with PHPStorm. This plugin contains a load of files with addons as well and it´s the time to use unit tests to check up before each commit in my github project. I took a ...
user avatar
  • 770
0 votes
1 answer
447 views

Write integration test for rest_pre_serve_request

I have a method that is hooked to the rest_api_init /** * Set allowed headers for the rest request */ public function set_allowed_rest_headers() { remove_filter( '...
user avatar
  • 1,954
0 votes
1 answer
346 views

Integration tests test script enqueue/register fails

This question is related to this SO post I'm testing a plugin, and in it I have public function enqueue_scripts() { $main_style = 'public/scripts/application.js'; wp_register_script( '...
user avatar
  • 1,954
3 votes
2 answers
578 views

Revert WordPress default options after a PHPUnit test has run

tl;dr: is there a way to make changes to WordPress options during a PHPUnit test suite execution, and have the changes reverted to the WordPress defaults before the next test suite is executed, ...
user avatar
  • 625
7 votes
1 answer
1k views

How to set up a user inside unit tests

I want to test some methods in my plugin, and some of them required user to be logged in. I searched everywhere but found nothing of use. I need to test a method that takes in logged in session ...
user avatar
  • 1,954
2 votes
1 answer
795 views

Why is WP_Mock not used instead of WP_UnitTestCase for writing unit tests by most plugins?

For writing unit-tests, why do most plugins use WP_UnitTestCase instead of popular libraries like WP_Mock? The reason I am asking this is because WP_UnitTestCase doesn't seem to be a unit test case ...
user avatar
1 vote
0 answers
106 views

Unit testing a plugin with a theme dependency

I wrote a plugin that adds functionality to load a content block (which is a page), and then passes it to a template part, which I did to avoid overloading the core WP global post. I'm trying to unit ...
user avatar
1 vote
1 answer
588 views

How to retain table data in wordpress phpunit tests

I was wondering is there a way that phpunit for woprdress do not delete any data which i created during test?
user avatar
  • 11
0 votes
2 answers
591 views

PHPUnit via WP-CLI: Warning: Cannot modify header information ... bootstrap.php:68

I'm trying to setup unit tests for a plugin I am developing. I just followed the steps at... https://make.wordpress.org/cli/handbook/plugin-unit-tests/ However, when I run phpunit I get the ...
user avatar
5 votes
1 answer
848 views

Writing a unit test for add_menu_page

I am writing a few unit tests for a plugin that I have developed. However I seem to be hung up on testing one (seemingly) simple piece of code. In the plugin I've written I have registered an admin ...
user avatar
  • 63
9 votes
1 answer
24k views

Access post from post id in URL

I'm doing unit tests for my plugin, and in my unit test, I check some basic things such as posting a custom post type. My script can create new post, and I can retrieve the ID of the posted item (I'm ...
user avatar
3 votes
2 answers
755 views

Is there any way of of making an admin-ajax request without the use of die()?

Pretty much what the title says. I have a custom plugin written, that relies on the use of admin-ajax to handle various forms and actions. That's all working fine, however as the various functions ...
user avatar
  • 150
3 votes
0 answers
673 views

Setting Up PHPUnit tests for WP development on Windows

I've been following the instructions in this article to try to set up some testing. I got as far as this instruction bash bin/install-wp-tests.sh wordpress_test root 'mypass' localhost latest before ...
user avatar
0 votes
2 answers
137 views

Installing WordPress Theme Unit Data

I'm following the "Building Themes from Scratch Using Underscores (2016)" tutorial on Lynda.com and at around 00:35 he suggests downloading the Theme Unit Test Data from WordPress. I've downloaded the ...
user avatar
1 vote
2 answers
3k views

What is the best way to create a factory for unit test objects?

I am trying to learn TDD and am struggling with creating factories for custom objects. For instance, if I have a custom user type and all users of that type must have a specific capability, it's ...
user avatar
0 votes
1 answer
793 views

How to do an unit test for the admin dashboard

I am trying to do unit test on codeception with wp-browser for a wordpress plugin based on a boilerplate. The problem that is in this line of code I initialize the class for admin only when is not an ...
user avatar
  • 43
4 votes
2 answers
147 views

What areas to Unit test while building a plugin?

I am a plugin developer with absolutely no industry experience. I have created a simple plugin to add multiple contributors to a post through metabox. I searched through tons of websites and I got to ...
user avatar
1 vote
1 answer
2k views

Error when setting up phpunit tests with wp-cli scaffold

I am trying to figure out the setup for unit tests for wordpress plugins. I am using Ubuntu. So far I have: Local WordPress Installation WP-CLI up and running Installed phpunit used wp scaffold ...
user avatar
4 votes
1 answer
546 views

Using wp_mail during an integration test

I'm working on some tests for WordPress, and I discovered that WordPress' PHPUnit automatically captures and doesn't send emails (using MockPHPMailer) when I try to use wp_mail. I've looked, but I ...
user avatar
  • 317
0 votes
1 answer
44 views

Set up PHPUnit with WordPress: The WordPress tests are 3 - 4 years old; does it matter?

I'm trying to set up PHPUnit with WordPress, and the tutorial I found tells me to download this repository so that I can bootstrap the WordPress environment to run my tests. The only issue I have is ...
user avatar
  • 423
7 votes
1 answer
897 views

Unit tests - Dealing with dependencies

This might be seen as a corollary of Testing hooks callback. The issue: I want to test a class which creates a new instance of a My_Notice class defined outside the plugin (let's call it the "Main ...
user avatar
0 votes
1 answer
541 views

Unable to find PHPUnit code coverage stats

Where should I check for the code coverage stats when using PHPUnit for WordPress? My WordPress setup: I have setup WordPress using VVV and my testing suite includes PHPUnit and WP-CLI. How I ...
user avatar
7 votes
1 answer
907 views

Writing test cases for a WordPress Plugin that has translations

What is my aim? I'm looking forward to write test cases for the available translations of a WordPress plugin. My approach: I have setup WordPress using VVV and my testing suite includes PHPUnit and ...
user avatar
3 votes
2 answers
1k views

Install and load additional plugins when running unit tests

I want to write unit tests for a plugin. I have used WP-CLI to scaffold the test WordPress instance and can successfully run tests. The plugin I'm writing unit tests for is a site specific plugin ...
user avatar
  • 899
8 votes
1 answer
852 views

Proper unit testing in WordPress

There are different threads available for Unit Testing in WordPress. As far as concern, there should be a proper unit testing for every piece of customization done in WordPress. I have checked couple ...
user avatar
2 votes
2 answers
20k views

Fatal error: Maximum execution time of 60 seconds exceeded in H:\wamp\www\custom\wp-includes\class-wp-http-curl.php

I imported the Theme Unit Test xml file with the wordpress importer. But all of the data is imported as pages. I am following an online tutorial to build a wordpress theme development and I believe ...
user avatar
  • 101
2 votes
1 answer
86 views

Using factory.php outside of trunk for unit testing

I have written a plugin, and am going through the process of setting up some unit tests for it. Inside of my unit tests I am utilizing the WP_UnitTest_Factory class to help with the process of ...
user avatar
  • 989
7 votes
2 answers
2k views

Wordpress Unit Testing - Cannot Create Tables

I'm using PHPUnit to Unit Test my WP plugin on top of the WP testing suite. Everything works fine except that when I try to create a table via the setUp method, the table doesn't get created. Here's ...
user avatar
  • 93
4 votes
3 answers
555 views

Unit testing to simulate the loop

How do you construct unit tests with PHPUnit and the WordPress test framework that simulate being in the loop? For instance, if I want to test a function that is only able to be called in the loop ...
user avatar
1 vote
0 answers
136 views

Is there a way to mock update_option for unit testing?

I'm currently building some unit tests, and have just started playing around with WP_Mock. I'd like to test a function that fetches the site description; I want to test if it responds as expected when ...
user avatar
  • 296
1 vote
2 answers
1k views

PHPUnit test plugin activation

I'm trying to test if my plugin activates properly with PHPUnit. I have used boilerplate structure generated on this site and added this test: class PluginTest extends WP_UnitTestCase { function ...
user avatar
  • 125