woocommerce/tests
Mike Jolley 20d7603dd6 Merge branch 'fix/21034' 2019-03-12 13:47:08 +00:00
..
bin Merge pull request #22634 from outis/fix-sed-pw 2019-02-06 14:43:32 -02:00
cli Merge percent and cart percent coupon types 2016-12-14 11:07:08 +00:00
data phpcs 2019-01-21 08:11:40 -08:00
e2e-tests Update e2e tests database to WP 5.0.1 and WC 3.5.2 2018-12-19 13:00:55 +00:00
framework Merge branch 'fix/21034' 2019-03-12 13:47:08 +00:00
includes Add wp-http-testcase directly to the repository 2018-07-18 18:19:39 -03:00
unit-tests Merge branch 'fix/22937' 2019-03-12 13:27:13 +00:00
README.md Update: docmented install script's handling of metacharacters in passwords. 2019-02-01 14:45:29 -08:00
bootstrap.php Change WC_Unit_Test_Case to extend WP_HTTP_TestCase instead of WP_UnitTestCase 2018-07-19 14:17:06 -03:00

README.md

WooCommerce Unit Tests

Initial Setup

  1. Install PHPUnit by following their installation guide. If you've installed it correctly, this should display the version:

    $ phpunit --version
    
  2. Install WordPress and the WP Unit Test lib using the install.sh script. Change to the plugin root directory and type:

    $ 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.

Sample usages:

$ tests/bin/install.sh woocommerce_tests root root

#  The actual password only has a single backslash, but it's escaped
#  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.

Running Tests

Simply change to the plugin root directory and type:

$ 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.

You can run specific tests by providing the path and filename to the test class:

$ phpunit tests/unit-tests/api/orders

A text code coverage summary can be displayed using the --coverage-text option:

$ phpunit --coverage-text

Writing Tests

  • Each test file should roughly correspond to an associated source file, e.g. the formatting/functions.php test file covers code in the wc-formatting-functions.php file
  • Each test method should cover a single method or function with one or more assertions
  • A single method or function can have multiple associated test methods if it's a large or complex method
  • 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()
  • 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
  • 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.
  • 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.

Automated Tests

Tests are automatically run with Travis-CI for each commit and pull request.

Code Coverage

Code coverage is available on Scrutinizer and Code Climate which receives updated data after each Travis build.