This document summarizes a presentation about testing in Craft CMS. The presentation covers:
1) The types of tests including unit, functional, and acceptance tests and how they help projects.
2) How to set up and run Codeception tests in Craft including installing Codeception, setting up directories and files, and adding tests.
3) Tips for successful testing like using fixtures, offloading tests to a queue, and different types of tests like email, Twig, performance and accessibility testing.
5. Agenda
1. How can tests help?
Are they worth the effort?
2. Types of tests.
3. Craft and Codeception
4. How to run Codeception tests in
Craft.
5. Tips for successful testing.
6. Testing modules and plugins.
7. Mock data with Fixtures.
8. Offloading tests to Queue.
9. Other kinds of tests.
10. Wrap up.
6. How can tests help?
Edge Cases
New Developers
You*
(one year later on same project)
7. Are they worth the effort?
Scale your level of effort
- One and done? (prob not)
- Maintenance & kicking can down the
road? (might be worth it)
- Crucial workflow, several developers?
(most def)
- Bleeding edge? (Yes but only stable)
8. Types of Tests
Manual tests (you already run these)
Refresh page, submit form, click stuff.
Log what happens.
Somewhat measurable.
Somewhat repeatable.
9. Types of Tests
Unit tests
Test specific results from a single “action”.
Test results of a function, possibly classes.
Measurable, repeatable.
In Craft, unit tests will primarily cover Services.
Test high in the call trace.
Save Controller method testing for functional.
10. Types of Tests
Functional / Acceptance tests
Test complete package from USER perspective.
Primarily will test your Craft Controller methods.
Typical Controller functional flow:
1) Process a request.
2) Invoke Craft services (Unit test)
3) Return a response
11. Testing is all about strategy and
approaches. Manual testing and automated
testing work best together. You can use
your judgement to detect/prevent issues
that computers cannot see whilst computers
can execute many tests in short time.
Tip:
Pixel & Tonic
13. Codeception in Craft
Fully featured PHP testing suite.
Requires Craft v3.2
Possible prior, requires bootstrapping
Yii Codeception module yourself.
14. How to run Codeception
tests in Craft
1. Installing Codeception.
2. Setting up required directories.
3. Setting up required files.
4. Setting up required config.
5. Codeception requirements.
6. Adding a test.
7. Running a test.
27. Adding a Test.
Add /tests/unit/ExampleTest.php
Codeception will use
UnitTester and Craft
classes to check the
Craft Edition for
“Pro” using
$this->assertSame(…).
33. Tips for Successful
Testing
Maintain your Database using
cleanup and transaction.
Cleanup ensures removal of any
Fixture data in your DB during your test.
Transaction ensures any DB changes are
rolled back using a Yii2 transaction.
34. Tips for Successful
Testing
Use .gitignore for /storage
And also new /tests/_output/.
Stick to the namespaces conventions
unit, functional, acceptance.
35. Testing Modules &
Plugins
Modules: once registered as normal
the module will be loaded and available
via MyModule::getInstance() to your test suites.
Plugins: Register in codeception.yml
then the plugin will be loaded and available
via MyPlugin::getInstance() to your test suites.
37. Mock Data with Fixtures
Fixtures: used to setup data in a test suite
That is predictable and the
same for each test run.
Craft gives us extras for Fixtures of Craft data:
Element, Asset, Category, Entry, Global set, Tag,
User and Element fixtures.
Extend crafttestfixtureselements…Fixture
38. Offloading Tests to Queue
1. Ensure your test class has a $tester property.
2. Call the following method:
First argument is the class of your job.
Second argument is any arguments
to pass to your job.
39. Other Kinds of Tests
-Testing emails with Mailtrap
-Visual Regression testing
tools and services
-Form Testing
-Testing at Twig level
-Performance, A11y tests (in
browser)
42. Testing Emails with
Mailtrap
Now all emails going out from Craft
including System messages and any
custom methods, will land at Mailtrap
in your account for testing and debugging.
*Free account = 500/month limit