How can I do unit testing with PHPUnit using the latest Local Beta?
Thanks
How can I do unit testing with PHPUnit using the latest Local Beta?
Thanks
Hi David,
Good question.
The first order of business is to right-click on the site in Localās sidebar and go to āOpen Site Shell.ā Doing so will set the necessary environment variables so your shell will use the appropriate binary versions and configs for PHP, MySQL, etc.
Once youāve done that, how you want to run PHPUnit comes up to a matter of preference and/or the project. If you have more details on how your project is set up I may be able to provide more pointers.
If you want to simply be able to run phpunit
at any time, you may be interested in installing PHPUnit globally like so: https://phpunit.readthedocs.io/en/8.5/installation.html#global-installation
@afragen has a great writeup on how he uses Local 5 and newer for WordPress Core Development: https://thefragens.com/wp-core-dev-with-local-lightning/. There are a few tidbits about PHPUnit in there that may be useful.
Thanks clay.
So, I want to be able to run phpunit on plugin development to test my plugins.
What I did so far was to install phpunit globally on OSX and then I Open Site Shell and run:
wp scaffold plugin-tests plugin-name.
Then I tried to run (Iām followin this https://make.wordpress.org/cli/handbook/plugin-unit-tests/):
bin/install-wp-tests.sh wordpress_test root '' localhost latest
But I got the error:
mysqladmin create wordpress_test --user=root --password= --host=localhost --protocol=tcp
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (61)'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'
Should I redirect some ports?
Basically I would like to use the scaffold functions of wp, so that it prepares everything for testing my plugin on a test database.
Thanks
Interesting link from @afragen. Checking now the script he mentioned there for installing phpunit on Local Lightning. Should the process be the same for Local Beta?
@dbeja the process is identical.
Localās default MySQL password is root
rather than a blank string.
Also, this reminds meā¦ That script doesnāt work great with Localās custom MySQL socket path.
Youāll need to run the following instead to skip database creation.
./bin/install-wp-tests.sh testing root root localhost latest true
Then, run mysql -e "CREATE DATABASE testing;"
No port redirection necessary since all of the processes are native and not going through Docker.
Yup! Local Beta is the same as Local [Lightning], it just gets more frequent updates
@afragen, I tried now to run just your script, but I got the error:
ERROR The WordPress directories for PHPUnit are not set
Could this be because I want to use this just for plugin unit test and not for code development?
Thanks, @clay, will test with your changes.
@dbeja did you start off with a fresh site on Local, then open the site shell, then run the script?
Yes, fresh start.
Can you make an issue on https://github.com/afragen/setup-phpunit/issues
Yes, will do it now. Thanks
This is what I see.
Checking if database wordpress_test exists
ERROR 1049 (42000) at line 1: Unknown database āwordpress_testā
Creating database wordpress_test
@dbeja FWIW these instructions are ONLY for MacOS, just making sure weāre all on the same page.
I tested now again with a new site, and now it worked and I also got the same message you mentioned above. Maybe I thought it was a fresh site and it wasnāt.
@clay I still would like to see these scripts turned into a Local Add-on. But I need lots of help.
Ok, phpunit itās now working with my plugins
So, in summary, what I did was:
wp scaffold plugin-tests plugin-name
./bin/install-wp-tests.sh testing root root localhost latest true
mysql -e "CREATE DATABASE testing;"
curl -o setup-phpunit.sh https://raw.githubusercontent.com/afragen/setup-phpunit/master/setup-phpunit.sh
bash setup-phpunit.sh
Yes, @afragen, that would be great. Iām starting now to look into Local Add-on development, so I would like to help with that.
Agreed! The Add-ons tab will be going back into Local in a week or two and Iād love to get more Add-ons published.
I just added Integrate script into Local Add-on Ā· Issue #2 Ā· afragen/setup-phpunit Ā· GitHub so we can get the ball rolling.
For some reason, I get the message āNo tests executed!ā but phpunit seems to be running normally, so it might be some configuration on my side, but Iāll investigate.
If youāre looking to run tests on a specific plugin you will need to cd
into the plugin directory and then run phpunit
Never mind, yes, I was on the plugin folder, but I was using the dummy test-sample.php that is scaffolded with wp, but on the configuration that file is excluded
Now I get the correct output.