|
1 | 1 | # Testing HWP Previews |
2 | 2 |
|
3 | | -This plugin uses [Codeception](https://codeception.com/) with [WPBrowser](https://wpbrowser.wptestkit.dev/) for automated testing. |
4 | | -Tests are organized into suites for integration (wpunit), functional, and acceptance testing. |
| 3 | +## Table of Contents |
5 | 4 |
|
6 | | ---- |
7 | | - |
8 | | -## Test Suites |
9 | | - |
10 | | -- **wpunit**: Unit/integration tests with WordPress loaded. |
11 | | -- **functional**: Simulates web requests, runs WordPress in a test environment. |
12 | | -- **acceptance**: Browser-based tests (WPBrowser/WPWebDriver). |
13 | | - |
14 | | -Configuration files for each suite are in the `tests/` directory (e.g., `wpunit.suite.dist.yml`). |
15 | | - |
16 | | ---- |
17 | | - |
18 | | -## Local Test Environment |
19 | | - |
20 | | -The plugin provides scripts to set up a local WordPress environment for testing, using Docker and environment variables defined in `.env.dist`. |
21 | | - |
22 | | -### Prerequisites |
23 | | - |
24 | | -- Docker (for local environment) |
25 | | -- Composer |
26 | | -- Node.js (for building assets, if needed) |
27 | | - |
28 | | ---- |
29 | | - |
30 | | -## Setup |
31 | | - |
32 | | -1. **Copy and configure environment variables:** |
33 | | - |
34 | | - ```bash |
35 | | - @TODO |
36 | | - cp .env.dist .env |
37 | | - # Edit .env as needed for your local setup |
38 | | - ``` |
39 | | - |
40 | | - |
41 | | -2. **Set up the test WordPress environment:** |
42 | | - |
43 | | - ```bash |
44 | | - bin/install-test-env.sh |
45 | | - ``` |
46 | | - |
47 | | - This script will: |
48 | | - - Create the test database (unless `SKIP_DB_CREATE=true`) |
49 | | - - Download and install WordPress in the directory specified by `WORDPRESS_ROOT_DIR` |
50 | | - - Symlink the plugin into the WordPress plugins directory |
51 | | - - Activate the plugin and set up test data |
| 5 | +- [Overview](#overview) |
| 6 | + - [Directory Structure](#directory-structure) |
| 7 | + - [Technologies](#technologies) |
| 8 | +- [Usage](#usage) |
| 9 | + - [Running Tests](#running-tests) |
| 10 | + - [GitHub Actions](#github-actions) |
| 11 | +- [Setup Tests Locally](#setup-tests-locally) |
52 | 12 |
|
53 | 13 | --- |
54 | 14 |
|
55 | | -## Running Tests |
56 | | - |
57 | | -Currently the plugin has the following suite of tests |
| 15 | +## Overview |
58 | 16 |
|
59 | | -1. WP Unit Tests - (Unit and Integration Tests) |
60 | | -2. E2E Tests - Playright tests |
| 17 | +HWP Previews comes with automated tests for unit, integration, and acceptance (E2E) scenarios. |
61 | 18 |
|
62 | | -### WPUnit (WordPress-aware Unit/Integration) Tests |
| 19 | +## Directory Structure |
63 | 20 |
|
64 | | -You can also run WPUnit tests using Composer scripts: |
| 21 | +A list of related files and directories for testing: |
65 | 22 |
|
66 | | -```bash |
67 | | -composer run test:unit |
68 | | -``` |
| 23 | +```text |
| 24 | +bin/ |
| 25 | +├── install-test-env.sh # Set up test WP environment |
| 26 | +├── run-codeception.sh # Run Codeception tests |
| 27 | +├── run-e2e.sh # Run E2E (Playwright) tests |
| 28 | +├── run-coverage.sh # Generate coverage reports |
| 29 | +└── local/ |
| 30 | + ├── run-unit-tests.sh # Run unit tests in Docker with Codeception |
| 31 | + ├── run-e2e-tests.sh # Run e2e tests in Docker with Playwright |
| 32 | + ├── run-qa.sh # Run php code quality checks with PHPStan, Psalm and PHPCS |
| 33 | + ├── run-wpunit.sh # Run WPUnit tests in Docker |
| 34 | + └── run-functional.sh # Run functional tests in Docker |
69 | 35 |
|
70 | | -To generate coverage reports: |
| 36 | +tests/ |
| 37 | +├── _data/ # Test data (e.g. DB dumps) |
| 38 | +├── _envs/ # Environment configs |
| 39 | +├── _output/ # Test output (logs, coverage) |
| 40 | +├── _support/ # Helper classes, modules |
| 41 | +├── wpunit/ # WPUnit (WordPress-aware unit/integration) test cases |
| 42 | +├── wpunit.suite.dist.yml |
| 43 | +└── wpunit/ |
| 44 | + └── bootstrap.php # Bootstrap for WPUnit tests |
71 | 45 |
|
72 | | -```bash |
73 | | -composer run test:unit:coverage |
| 46 | +.env.dist # Example environment variables for testing |
| 47 | +codeception.dist.yml # Main Codeception config |
74 | 48 | ``` |
| 49 | +--- |
75 | 50 |
|
76 | | -To generate an HTML coverage report: |
| 51 | +## Technologies |
77 | 52 |
|
78 | | -```bash |
79 | | -composer run test:unit:coverage-html |
80 | | -``` |
81 | | -> [!NOTE] |
82 | | -> HTML code coverage can be found here [tests/_output/coverage/index.html](tests/_output/coverage/index.html) |
| 53 | +We use the following technologies to run our tests: |
83 | 54 |
|
| 55 | +- [Codeception](https://codeception.com/) |
| 56 | +- [WPBrowser](https://wpbrowser.wptestkit.dev/) |
| 57 | +- [WPUnit](https://github.com/lipemat/wp-unit) |
| 58 | +- [Docker](https://www.docker.com/) |
| 59 | +- [Composer](https://getcomposer.org/) |
| 60 | +- [Playwright](https://playwright.dev/) |
| 61 | +- [npm](https://www.npmjs.com/) |
84 | 62 |
|
85 | | -### E2WTests |
| 63 | +--- |
86 | 64 |
|
87 | | -Run browser-based acceptance tests: |
| 65 | +## Usage |
88 | 66 |
|
89 | | -```bash |
90 | | -sh bin/local/run-e2e-tests.sh coverage |
91 | | -``` |
| 67 | +Currently, the plugin has the following suite of tests: |
92 | 68 |
|
93 | | -### All Tests |
| 69 | +1. **WP Unit Tests** – Unit and Integration Tests |
| 70 | +2. **E2E Tests** – Acceptance tests |
94 | 71 |
|
95 | | -To run all suites: |
| 72 | +### Running Tests |
96 | 73 |
|
97 | | -```bash |
98 | | -composer run test |
99 | | -# or |
100 | | -vendor/bin/codecept run |
101 | | -``` |
| 74 | +| Command | Description | |
| 75 | +|------------------------------------------|----------------------------------------------------------| |
| 76 | +| `composer run test:unit:coverage` | Run WPUnit (unit/integration) tests with coverage report | |
| 77 | +| `composer run test:unit:coverage-html` | Generate an HTML code coverage report | |
| 78 | +| `composer run test:e2e` | Run end-to-end (E2E) acceptance tests | |
| 79 | +| `composer run test` | Run all available test suites | |
102 | 80 |
|
103 | 81 | --- |
104 | 82 |
|
105 | | -## Code Coverage |
106 | | - |
107 | | -To generate code coverage reports (requires Xdebug or PCOV): |
| 83 | +## GitHub Actions |
108 | 84 |
|
109 | | -```bash |
110 | | -# Example for wpunit suite |
111 | | -SUITES=wpunit COVERAGE=1 bin/run-codeception.sh |
112 | | -``` |
| 85 | +We have a few checks which run for a new PR being merged to main |
113 | 86 |
|
114 | | -Coverage output will be in `tests/_output/` or as specified by `COVERAGE_OUTPUT`. |
| 87 | +| Workflow | Description | Link | |
| 88 | +|-------------------------|---------------------------------------------|----------------------------------------------------------------------| |
| 89 | +| Code Quality | Runs static analysis and linting checks | [View Workflow](../../actions/workflows/code-quality.yml) | |
| 90 | +| E2E Tests | Runs Playwright end-to-end acceptance tests | [View Workflow](../../actions/workflows/e2e.yml) | |
| 91 | +| Codeception (WPUnit) | Runs unit and integration tests | [View Workflow](../../actions/workflows/codeception.yml) | |
115 | 92 |
|
116 | | ---- |
117 | 93 |
|
118 | | -## Useful Scripts |
| 94 | +> **INFO:** |
| 95 | +> All tests are automatically run on every pull request via GitHub Actions. You can review test results and logs directly in the "Checks" tab of your PR on GitHub. |
119 | 96 |
|
120 | | -- `bin/install-test-env.sh` — Sets up the WordPress test environment. |
121 | | -- `bin/run-codeception.sh` — Runs Codeception tests inside the plugin directory. |
122 | | -- `bin/local/run-unit-tests.sh` — Runs unit tests in Docker. |
123 | | -- `bin/local/run-qa.sh` — Runs code quality checks (PHPStan, PHPCS, Psalm). |
| 97 | +> **IMPORTANT:** |
| 98 | +> Test coverage for WP Unit Tests is 95%. Any new code will require tests to be added in order to pass. |
124 | 99 |
|
125 | 100 | --- |
126 | 101 |
|
127 | | -## Notes |
128 | | - |
129 | | -- The test database will be reset during setup. **Do not use a database with important data.** |
130 | | -- You can customize which suites to run by setting the `SUITES` environment variable. |
131 | | -- See `.env.dist` for all available environment variables and their descriptions. |
| 102 | +## Setup Tests Locally |
132 | 103 |
|
133 | | ---- |
134 | | - |
135 | | -```text |
136 | | -tests/ |
137 | | -├── _data/ # Test data (e.g. DB dumps) |
138 | | -├── _envs/ # Environment configs |
139 | | -├── _output/ # Test output (logs, coverage) |
140 | | -├── _support/ # Helper classes, modules |
141 | | -├── wpunit/ # WPUnit (WordPress-aware unit/integration) test cases |
142 | | -├── wpunit.suite.dist.yml |
143 | | -└── wpunit/ |
144 | | - └── bootstrap.php # Bootstrap for WPUnit tests |
145 | | -
|
146 | | -bin/ |
147 | | -├── install-test-env.sh # Script to set up test WP environment |
148 | | -├── run-codeception.sh # Script to run Codeception tests |
149 | | -└── local/ |
150 | | - ├── run-unit-tests.sh # Run unit tests in Docker |
151 | | - └── run-qa.sh # Run code quality checks |
152 | | -
|
153 | | -.env.dist # Example environment variables for testing |
154 | | -codeception.dist.yml # Main Codeception config |
155 | | -``` |
| 104 | +@TODO |
0 commit comments