* Coupon mutations implemented and tested * Vendor files no longer versioned and package workflow implemented
7.1 KiB
title, metaTitle, metaDescription
| title | metaTitle | metaDescription |
|---|---|---|
| Testing (Quick-Start Guide) | WooGraphQL Testing Quick-Start Guide | WooGraphQL Docs | AxisTaylor | A simple guide to get started testing with WooGraphQL. |
Unit Tests
Until the documentation is in full effect, it's recommended that a GraphiQL-based tool like WPGraphQL IDE be used to view the GraphQL schema, an alternative to this is viewing the unit tests located in tests/wpunit directory. Which are constantly updated along with the project. If you're interested in contributing when I begin accepting contribution or simply want to run the tests. Follow the instruction below.
Prerequisties
Setup
- First copy the
.env.testingfile as.env - Next open
.envand alter to make you usage.
# WordPress admin configurations
ADMIN_EMAIL=admin@woographql.local
ADMIN_USERNAME=admin
ADMIN_PASSWORD=password
ADMIN_PATH=/wp-admin
# WordPress local configurations
WP_CORE_DIR=local/public
PLUGINS_DIR=local/public/wp-content/plugins
MUPLUGINS_DIR=local/public/wp-content/mu-plugins
THEMES_DIR=local/public/wp-content/themes
# WordPress database configurations
DB_NAME=wordpress
DB_HOST=app_db
DB_PORT=3306
DB_USER=wordpress
DB_PASSWORD=password
ROOT_PASSWORD=password
WP_TABLE_PREFIX=wp_
SKIP_DB_CREATE=false
# Extra variables/constants.
GRAPHQL_JWT_AUTH_SECRET_KEY=testingtesting123
STRIPE_API_PUBLISHABLE_KEY=""
STRIPE_API_SECRET_KEY=""
PHPUNIT_VERSION=":<=9.4.4"
- Typical you should only have to change the WordPress database configurations to use local testing.
- Once you have finished modifying the
.envfile. Runcomposer install-test-envfrom the project directory. This will install WordPress + Codeception w/ WPBrowser, as well as setup the database if needed. - Upon success you can begin running the tests.
Running tests locally
To run the tests use the command vendor/bin/codecept run [suite [test [:test-function]]].
If you use the command with at least a suite specified, Codeception will run all tests, however this is not recommended. Running a suite vendor/bin/codecept run wpunit or a test vendor/bin/codecept run CouponQueriesTest is recommended. Running a single test-function like vendor/bin/codecept run ProductQueriesTest:testProductsQueryAndWhereArgs is also possible.
To learn more about the usage of Codeception with WordPress view the Documentation
Functional and Acceptance Tests (Server or Docker required)
Running functional and acceptance tests requires that WordPress installation being used for testing be accessible thru some kind of URL/Address and then setting that URL/Address in the codeception.dist.yml file or in the .env file as WORDPRESS_DOMAIN/WORDPRESS_URL.
Note: The codeception.dist.yml should be left unchanged and a copy named codeception.yml should be used.
Running the install-test-env alone does not configure a server to point at the Wordpress installation it creates, you're two options for doing this.
- Configure an Apache or Nginx server block and point it at the WordPress installation created by the
install-test-envscript. This isn't a very flexible or quick method. - Use the Docker configurations in the project to push the installation into a docker network and expose it's docker container's IP as the URL/Address. This is the recommended option if Docker/Docker-Compose is available to you. The project includes some simple composer scripts that enable you to run all at once or filter specific tests for speed, test isolation, or XDebug Stepping Debug.
The composer scripts for using dockers are.
docker-buildbuilds this the Docker Image for thewoographql/wordpresscontainer that will house the WordPress installation and tests.docker-run-appspins up the docker network. This can be used for live debug as well as cli test. The WordPress installation will be accessible from a URL provided in thewoographql/wordpresscontainer log in you terminal.docker-run-testing-dbadd a cloned MySQL instance of the one created indocker-run-appfor testing, this is to be run after the docker network as been created usingdocker-run-appand before the tests are run inside of thewoographql/wordpresscontainer. Note: This should not be used directly. Seedocker-run-testbelow.docker-set-main-dbconfigures the wp-config.php of wordpress installation to point at the main MySQL container. This is for returning to live development using the browser after running the tests in cli.docker-set-testing-dbis essentially the samedocker-set-main-dbjust for the testing database. However this script stalls and looks for the testing database to be available. Note: This should not be used directly. Seedocker-run-testbelow.docker-run-testis the primary tester script and the script you'll probably call the most. This script essentially just runscodecept run $FILTERin thewoographql/wordpress, however before that it runsdocker-run-testing-db, anddocker-set-testing-dbto ensure all the need players are set.FILTERis a shell variable that can be what parameter you what to pass to the run command except the--envor--no-exitoptions. For usage see this example.FILTER="wpunit CartMutationsTest:testAddToCartMutationWithProduct --debug" composer docker-run-testdocker-run-test-standalonesetups the docker network, run all the tests, and pulls down the network. Primarily for CI.
Running tests with Docker/Docker-Compose
Running the tests in rather simple, but you may need two terminal windows depending on you method.
- (Requires two terminals in the project root directory) In one terminal run
composer docker-build && composer docker-run-app. Wait until you see the logtestable_app_x_xxxxxxxxxxxx | WordPress app located at http://xxx.xxx.xxx.xxx, then you're ready to run the tests and can leave this terminal running and move to your second one. In your second terminal you can run the by executingcomposer run-testThis will run all the tests at once with no options passed to thecodecept runcommand. This can be alter withFILTERvariable mention in the last section. The first time you run this command it will be delayed due to having to setup the test database. Also one last thing to note is if you when to switch to live development/debugging in the browser have running the test this way. Runcomposer docker-set-main-dbbefore you do. - This other method is even alot more streamline and only needs one command
composer docker-run-test-standalone. See description of what it does in the last section.
Using docker-compose to run a local installation for live testing
This is rather simple just like with testing using docker ensure that env.dist and codeception.dist.yml are untouched.
- Run
composer docker-build && composer docker-run-app. - Wait until you see the log
testable_app_x_xxxxxxxxxxxx | WordPress app located at http://xxx.xxx.xxx.xxx. - Navigate to the provided address.