Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [unit-tests]

Automated tests for the smallest parts of code.

0
votes
1answer
18 views

Populate tests database with products

I am trying to test my plugin which uses WooCommerce products, for that I need to populate tests database with dummy product during tests. How can I do it like WordPress itself populating posts table ...
0
votes
0answers
12 views

Unit test for wordpress plugin

I have followed wordpress office blog -->https://make.wordpress.org/cli/handbook/plugin-unit-tests/ to implement unit testing in plugin. I have been able to write a simple test on one of my method of ...
0
votes
0answers
23 views

Integration testing rest callback which has wp_send_json_error in it?

I have a custom login endpoint, and in that endpoint's callback I have some checks (like if user isn't activated). If that check fails I will return a wp_send_json_error(). I also have a ...
0
votes
0answers
17 views

Faster Unit Testing in WordPress?

I'm using WordPress Unit Testing to develop a plugin. The Unit Testing is the one provided by WP-CLI with the wp scaffold plugin-tests command: https://make.wordpress.org/cli/handbook/plugin-unit-...
0
votes
0answers
22 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 ...
1
vote
0answers
20 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. ...
1
vote
1answer
42 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(...
4
votes
0answers
29 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 ...
0
votes
1answer
66 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( '...
0
votes
0answers
16 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( '...
3
votes
2answers
55 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, ...
2
votes
1answer
59 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 ...
1
vote
1answer
214 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 ...
1
vote
0answers
41 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 ...
0
votes
1answer
189 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?
0
votes
2answers
203 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 ...
5
votes
1answer
271 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 ...
0
votes
0answers
4k 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 ...
2
votes
2answers
153 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 ...
3
votes
0answers
253 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 ...
0
votes
2answers
84 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 ...
1
vote
2answers
848 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 ...
0
votes
1answer
295 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 ...
5
votes
2answers
94 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 ...
1
vote
0answers
305 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 ...
3
votes
1answer
192 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 ...
0
votes
1answer
34 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 ...
7
votes
1answer
307 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 ...
0
votes
1answer
360 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 ...
8
votes
1answer
448 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 ...
2
votes
2answers
656 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 ...
8
votes
1answer
445 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 ...
2
votes
2answers
19k 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 ...
2
votes
1answer
62 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 ...
5
votes
1answer
778 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 ...
4
votes
3answers
301 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 ...
1
vote
0answers
57 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 ...
1
vote
2answers
511 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 ...
7
votes
1answer
811 views

When unit testing a plugin, does the plugin need to be in the wp-content/plugins directory of the WordPress tests install?

I'm currently setting up unit testing for my plugin. In my tests/phpunit/bootstrap.php file I have: require_once $wp_tests_dir . '/includes/functions.php'; tests_add_filter( 'muplugins_loaded', ...
3
votes
2answers
408 views

Using Travis CI to run unit tests and running phpunit locally [closed]

Is there a difference between Travis CI running my PHPUnit tests and me using the WP-CLI to generate unit test files and running phpunit locally? I'm wondering if the duplicate effort is necessary or ...
5
votes
1answer
1k views

Unit testing in the WordPress backend (is_admin() is true)

I've asked this on IRC, Twitter, Slack — am utterly astonished by how difficult it is to locate the answer to this: I have a bunch of plugin code that only runs when is_admin() returns true, i.e., ...
2
votes
1answer
889 views

DataBase connection problem with PHPUnit and WordPress

I'm trying to run PHPUnit to unittest a WordPress plugin, but the error below keeps showing up. Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /private/tmp/wordpress/wp-...
2
votes
2answers
341 views

How do I mock get_adjacent_post for testing

I'm trying to mock get_adjacent_post for unit testing but I'm not sure how to mock global function in PHPUnit $badCode = $this->getMockBuilder('get_adjacent_post') ->setMethods(...
2
votes
1answer
293 views

How can I properly set up dependencies in automated testing?

I'm trying to set up automated tests for my WP- and BP-dependent plugin, as described in: https://codex.buddypress.org/developer/automated-testing/ https://make.wordpress.org/core/handbook/automated-...
0
votes
1answer
35 views

Unregister Settings Error

Is it possible to unregister or unset an error that has been added using add_settings_error? What I am trying to do is run some unit tests on my settings validation function, but I'd like to reset any ...
0
votes
1answer
43 views

How can I link tests to GitHub issues?

I'm trying to run WordPress automated tests as described in this article. Predictably, I've been getting a lot of skipped tests marked S, because, according to the article, "this usually means that ...
6
votes
1answer
412 views

Plugin Unit Test for Table and Option Creation

I've started to incorporate unit testing in my build. I have a class that creates a database table and a database version option. I have no idea how to test if the table and option are being created. ...
1
vote
1answer
450 views

How exactly *does* unit testing work for themes?

As the majority of material on unit testing focuses on plugin development rather than theme development, I've hit a stumbling block when trying it out for myself. I've installed PHPUnit, grabbed the ...
1
vote
2answers
843 views

How to add an image for unit testing?

I have to test the function where it calls wp_get_attachment_image_src. How do I add an image for testing, because right now if I call this method it returns false.
1
vote
0answers
83 views

What make possible unit test plugins?

This is more a theory related question. I'm not looking at the practical ways to do this, as I can find plenty of example (official and not), which however does not explain how it is possible to ...