Update tests documentation

Update tests documentation as PHPUnit now should be installed and called using composer instead of
globally. This commit also includes some other minor changes to the
tests documentation like updating the link of the service used for code
coverage reports.
This commit is contained in:
Rodrigo Primo 2019-07-26 14:48:05 -03:00
parent ba517650a1
commit 9752c1f8a0
1 changed files with 13 additions and 16 deletions

View File

@ -2,28 +2,25 @@
## Initial Setup ## Initial Setup
1) Install [PHPUnit](http://phpunit.de/) by following their [installation guide](https://phpunit.de/getting-started.html). If you've installed it correctly, this should display the version: From the WooCommerce root directory (if you are using VVV you might need to `vagrant ssh` first), run the following:
1. Install [PHPUnit](http://phpunit.de/) via Composer by running:
``` ```
$ phpunit --version $ composer install
``` ```
2) Install WordPress and the WP Unit Test lib using the `install.sh` script. Change to the plugin root directory and type: 2. Install WordPress and the WP Unit Test lib using the `install.sh` script:
``` ```
$ tests/bin/install.sh <db-name> <db-user> <db-password> [db-host] $ tests/bin/install.sh <db-name> <db-user> <db-password> [db-host]
``` ```
The `<db-password>` will be set as given. Previously, you would have needed to escape certain characters (forward & backward slashes, and ampersand), but install.sh now escapes them when it needs to internally. You may still need to quote strings with backslashes to prevent them from being processed by the shell or other programs. You may need to quote strings with backslashes to prevent them from being processed by the shell or other programs.
Sample usages: Example:
$ tests/bin/install.sh woocommerce_tests root root $ tests/bin/install.sh woocommerce_tests root root
# The actual password only has a single backslash, but it's escaped # woocommerce_tests is the database name and root is both the MySQL user and its password.
# to prevent the shell and PHP from treating it as a backspace character
$ tests/bin/install.sh woocommerce_tests root 'a\\b/&'
# Previously, the password would have had to be passed as 'a\\\\b\/\&'
**Important**: The `<db-name>` database will be created if it doesn't exist and all data will be removed during testing. **Important**: The `<db-name>` database will be created if it doesn't exist and all data will be removed during testing.
@ -31,17 +28,17 @@ Sample usages:
Simply change to the plugin root directory and type: Simply change to the plugin root directory and type:
$ phpunit $ vendor/bin/phpunit
The tests will execute and you'll be presented with a summary. Code coverage documentation is automatically generated as HTML in the `tmp/coverage` directory. The tests will execute and you'll be presented with a summary.
You can run specific tests by providing the path and filename to the test class: You can run specific tests by providing the path and filename to the test class:
$ phpunit tests/unit-tests/api/orders $ vendor/bin/phpunit tests/unit-tests/importer/product.php
A text code coverage summary can be displayed using the `--coverage-text` option: A text code coverage summary can be displayed using the `--coverage-text` option:
$ phpunit --coverage-text $ vendor/bin/phpunit --coverage-text
## Writing Tests ## Writing Tests
@ -51,7 +48,7 @@ A text code coverage summary can be displayed using the `--coverage-text` option
* Use the test coverage HTML report (under `tmp/coverage/index.html`) to examine which lines your tests are covering and aim for 100% coverage * Use the test coverage HTML report (under `tmp/coverage/index.html`) to examine which lines your tests are covering and aim for 100% coverage
* For code that cannot be tested (e.g. they require a certain PHP version), you can exclude them from coverage using a comment: `// @codeCoverageIgnoreStart` and `// @codeCoverageIgnoreEnd`. For example, see [`wc_round_tax_total()`](https://github.com/woocommerce/woocommerce/blob/35f83867736713955fa2c4f463a024578bb88795/includes/wc-formatting-functions.php#L208-L219) * For code that cannot be tested (e.g. they require a certain PHP version), you can exclude them from coverage using a comment: `// @codeCoverageIgnoreStart` and `// @codeCoverageIgnoreEnd`. For example, see [`wc_round_tax_total()`](https://github.com/woocommerce/woocommerce/blob/35f83867736713955fa2c4f463a024578bb88795/includes/wc-formatting-functions.php#L208-L219)
* In addition to covering each line of a method/function, make sure to test common input and edge cases. * In addition to covering each line of a method/function, make sure to test common input and edge cases.
* Prefer `assertSame()` where possible as it tests both type & equality * Prefer `assertSame()` where possible as it tests both type and value
* Remember that only methods prefixed with `test` will be run so use helper methods liberally to keep test methods small and reduce code duplication. If there is a common helper method used in multiple test files, consider adding it to the `WC_Unit_Test_Case` class so it can be shared by all test cases * Remember that only methods prefixed with `test` will be run so use helper methods liberally to keep test methods small and reduce code duplication. If there is a common helper method used in multiple test files, consider adding it to the `WC_Unit_Test_Case` class so it can be shared by all test cases
* Filters persist between test cases so be sure to remove them in your test method or in the `tearDown()` method. * Filters persist between test cases so be sure to remove them in your test method or in the `tearDown()` method.
* Use data providers where possible. Be sure that their name is like `data_provider_function_to_test` (i.e. the data provider for `test_is_postcode` would be `data_provider_test_is_postcode`). Read more about data providers [here](https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers). * Use data providers where possible. Be sure that their name is like `data_provider_function_to_test` (i.e. the data provider for `test_is_postcode` would be `data_provider_test_is_postcode`). Read more about data providers [here](https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers).
@ -62,4 +59,4 @@ Tests are automatically run with [Travis-CI](https://travis-ci.org/woocommerce/w
## Code Coverage ## Code Coverage
Code coverage is available on [Scrutinizer](https://scrutinizer-ci.com/g/woocommerce/woocommerce/) and [Code Climate](https://codeclimate.com/github/woocommerce/woocommerce) which receives updated data after each Travis build. Code coverage is available on [Scrutinizer](https://scrutinizer-ci.com/g/woocommerce/woocommerce/) and [Codecov](https://codecov.io/gh/woocommerce/woocommerce/) which receives updated data after each Travis build.