A great way to learn which assertions to use for your tests is to take the equivalent comparative code and find the assertion that does the comparison work for you.
In this episode, I’m going to challenge you to do just that. I want you to take our test sample assertions, which are all
assertTrue(), look at the comparison code, and figure out which assertion can replace that code. You’ll be using the PHPUnit manual.
Then together, you and I will walk through the process together. We’ll look at the different assertions and see how they work. I’ll also show you the handful of asserts that I most commonly use.
What is an assert or assertion?
An assert or assertion is our testing method. We select the one based upon what we expect the test result to be. For example, if you want to test that a function returns an array, you can test that:
- It is an array data type.
- It has the structure or key(s) you expect it to have.
- It has the data values that you expect it should have.
assertTrue says that you expect the result to be a
true value. An
assertEquals says that you expect the result to be equal to some expected value that you define.
Don’t repeat yourself. Don’t repeat yourself. Don’t repeat yourself.
Total Lab Runtime: 04:26:49
- 1 Lab Introductionfree 13:13
- 2 How Testing Benefits Youfree 27:00
- 3 What is Testing?free 10:24
- 4 Unit vs. Integration Testsfree 17:44
- 5 Setup the Testing Environmentpro 12:21
- 6 PHPUnit & a Virtual Containerpro 08:37
- 7 Create the Composer.json Filepro 14:36
- 8 Configure PHPUnit for Both Test Suitespro 10:56
- 9 Setup Test Shortcuts in Composerpro 09:58
- 10 Setup Local's Web Serverfree 10:05
- 11 Unit Tests - Bootstrappro 14:34
- 12 Test Basicspro 12:24
- 13 Test Assertionspro 18:42
- 14 Unit Tests - Write Real Testspro 23:32
- 15 Integration Bootstrap - Overviewfree 09:01
- 16 Integration Bootstrap - Find the WordPress Test Suite's Directorypro 12:10
- 17 Integration Bootstrap - Defining Constantspro 04:06
- 18 Integration Bootstrap - Register Theme & Loadpro 07:41
- 19 Meet WP_UnitTestCasepro 12:05
- 20 Integration Tests - Write Real Testspro 15:18
- 21 Wrap it Upfree 02:22