---
title: How to Write Unit Tests Using PHPUnit and WP_Mock
description: Your guide to writing unit tests for WordPress code.
image: https://blog.avenuecode.com/hubfs/puzzle-geea4a738e_1280.jpg
---

[AvenueCode.com](https://avenuecode.com) [News](https://avenuecode.com/news) [Contact](https://avenuecode.com/contact)

[![Avenue Code Snippets Logo](https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/AC-Snippets---Black.png)](https://blog.avenuecode.com/?hsLang=en-us) *menu*

- Technology
  
  [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us) [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us) [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us) [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us) [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
  
  [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us) [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us) [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us) [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

- [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us)
- [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us)
- [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us)
- [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us)
- [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us)
- [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us)
- [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us)
- [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

# [How to Write Unit Tests Using PHPUnit and WP\_Mock](https://blog.avenuecode.com/how-to-write-unit-tests-using-phpunit-and-wp_mock)

- [Tweet](https://twitter.com/share)

*perm\_identity* [Luiz Henrique Santos](https://blog.avenuecode.com/how-to-write-unit-tests-using-phpunit-and-wp_mock/author/luiz-henrique-santos?hsLang=en-us)

*schedule* 4/13/22 2:00 PM

Do you write unit tests for your code? If you don't, you should. Unit tests are a fast, inexpensive, and efficient tool that helps guarantee code quality.

##### Why Do We Need Unit Tests?

The main purpose of unit tests is to test the smallest piece of a code, such as a function. Unit tests must focus on testing the system logic and avoid testing other function calls, database access, and external APIs. How do we do this without these calls? We can use mocks.

##### What is a Mock?

Mocking is used in unit tests when the unit being tested has external dependencies. Mock object simulates the external dependency behavior so that it is possible to focus the unit test on the logic of what is being tested. 

Mocking framework is a complement to unit testing framework. It helps developers build fast and efficient unit tests.

WP\_Mock is an API mocking framework that enables unit tests in WordPress. It is very helpful because WordPress has a lot of classes, functions, and hooks, so it would be hard to write unit tests without WP\_Mock.

##### Using WP\_Mock

WP\_Mock is installable as a composer dependency using this command:

```
composer require --dev 10up/wp_mock:0.4.2
```

After installation, you have to create a test class extending TestCase from WP\_Mock. The function setUp is responsible for running all necessary configurations before each test case, and it is common to add a require\_once call from the class that is being tested.  After each test, tearDown function is called, and you'll also be able to add some scripts to run in this portion of code, as shown below:

```
class Your_Class_Test extends WP_Mock\Tools\TestCase {public function setUp() {\WP_Mock::setUp();}public function tearDown() {\WP_Mock::tearDown();}}
```

##### Mocking WordPress Core Functions

You can also mock WordPress core functions using \\WP\_Mock::userFunction(). This function must receive as an argument the function that is being mocked. It's also optional to specify how many times it should be called, the arguments it should receive, and what is being returned.

Let’s imagine that you are testing a function that uses the WordPress get\_post function. The code below is an example of when function get\_post is called once, with array(1) as parameter, and the return being a post object:

```
public function test_uses_get_post() {$post = new \stdClass;$post->ID = 1;\WP_Mock::userFunction( 'get_post', array('times' => 1,'args' => array( $post->ID ),'return' => $post,) );}
```

WordPress objects can be mocked using another framework called Mockery, which is a dependency from WP\_Mock. It is useful to mock calls to wpdb or WP\_Query without accessing the database itself. The example below mocks WP\_Query object and asserts that function have\_posts returns false:

```
public function test_wp_query(): void {$wp_query = Mockery::mock(WP_Query' );$wp_query->shouldReceive( 'have_posts' )->andReturn( false );}
```

##### Conclusion

In today's environment, unit testing is an essential part of a good development process, especially since it's fast to run and ensures that your code is reliable. WP\_Mock is a framework that helps developers build tests in WordPress, providing tools to mock core functions and objects. WP\_Mock is especially helpful because it will likely reduce the time you spend building tests to improve performance.

 

##### References

"[What is Unit Testing?](https://smartbear.com/learn/automated-testing/what-is-unit-testing/)" SmartBear.

"[Unit Testing for Software Craftsmanship](https://www.telerik.com/products/mocking/unit-testing.aspx)." Telerik.

[WordPress API Mocking Framework](https://github.com/10up/wp_mock). GitHub.

---

### Author

# Luiz Henrique Santos

 Luiz Henrique Santos is a Full Stack PHP Developer at Avenue Code with 7+ years of experience in IT. He always tries to understand functionalities to propose the best solution while working to deadlines. He applies SOLID standards, develops code that is easy to read and change, and writes unit tests for everything he does!

---

### Related Posts

### How to Build Unit Tests Using Jest

[READ MORE](https://blog.avenuecode.com/how-to-build-unit-tests-using-jest?hsLang=en-us)

### How Heuristics Can Improve Your Tests

[READ MORE](https://blog.avenuecode.com/how-heuristics-can-improve-your-tests?hsLang=en-us)

### How to Use WireMock for Integration Testing

[READ MORE](https://blog.avenuecode.com/how-to-use-wiremock-for-integration-testing?hsLang=en-us)

### The Best Way to Use Request Validation in Laravel REST API

[READ MORE](https://blog.avenuecode.com/the-best-way-to-use-request-validation-in-laravel-rest-api?hsLang=en-us)

### Leave a Comment!

### Avenue Code Social

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Blog/facebook.png?t=1486470796564)](https://www.facebook.com/avenuecode)

[![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Blog/twitter.png?t=1486470796842)](https://twitter.com/AvenueCode)

[![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png?t=1486470796556)](https://www.linkedin.com/company/avenuecode/)

### Newsletter

Want to stay on top of all tips and news from Avenue Code?

### Popular Snippets

![Avenue Code-primary versions_logo white avenue code endorsement 2](https://blog.avenuecode.com/hs-fs/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png?width=1180&name=Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png "Avenue Code-primary versions_logo white avenue code endorsement 2")

### About Us

- [Who We Are](https://www.avenuecode.com/who-we-are)
- [What We Do](https://www.avenuecode.com/what-we-do)
- [Portfolio](https://www.avenuecode.com/portfolio)
- [Partners](https://www.avenuecode.com/partners)
- [News](https://www.avenuecode.com/news)
- [Events](https://www.avenuecode.com/events)
- [Blog](https://blog.avenuecode.com/)
- [Contact](https://www.avenuecode.com/contact)

### Our Offices

San Francisco

[+1 415 766 4178](tel:+553125161448) [ac.inquiries@avenuecode.com](mailto:brazil.info@avenuecode.com)

Belo Horizonte

[+55 31 2516 1448](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

São Paulo

[+55 11 3205 3232](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

### We're Hiring!

- [Belo Horizonte](https://www.avenuecode.com/who-we-are)
- [New York](https://www.avenuecode.com/what-we-do)
- [San Francisco](https://www.avenuecode.com/portfolio)
- [São Paulo](https://www.avenuecode.com/partners)

---

©2015 - 2017 Avenue Code

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Icons/facebook-2.png)](https://www.facebook.com/avenuecode) [![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Icons/twitter-2.png)](https://twitter.com/AvenueCode) [![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Icons/linkedin-2.png)](https://www.linkedin.com/company/avenue-code) [![Glassdoor Icon](https://blog.avenuecode.com/hubfs/Images/Icons/glassdoor-icon-1.png)](https://www.glassdoor.com/Overview/Working-at-Avenue-Code-EI_IE456173.11,22.htm) [![YouTube Icon](https://blog.avenuecode.com/hubfs/Images/Icons/youtube-2.png)](https://www.youtube.com/user/AvenueCodePlay)

Please enable JavaScript to view the [comments powered by Disqus.](http://disqus.com/?ref_noscript)

© 2026 Avenue Code

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Luiz Henrique Santos",
    "url" : "https://blog.avenuecode.com/author/luiz-henrique-santos"
  },
  "dateModified" : "2022-04-13T17:00:00.449Z",
  "datePublished" : "2022-04-13T17:00:00.000Z",
  "headline" : "How to Write Unit Tests Using PHPUnit and WP_Mock",
  "image" : [ "https://blog.avenuecode.com/hubfs/puzzle-geea4a738e_1280.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.avenuecode.com/how-to-write-unit-tests-using-phpunit-and-wp_mock",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_LOGO%20HORIZONTAL%20group%201-7.png"
    },
    "name" : "Avenue Code"
  }
}
```