Is it possible to use PHPUnit with Local Beta?

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 :slight_smile:

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

1 Like

@clay I still would like to see these scripts turned into a Local Add-on. But I need lots of help.

1 Like

Ok, phpunit it’s now working with my plugins :slight_smile:

So, in summary, what I did was:

  1. Create a new site on Local Beta
  2. Open site shell
  3. Run: wp scaffold plugin-tests plugin-name
  4. cd to plugin dir
  5. Run: ./bin/install-wp-tests.sh testing root root localhost latest true
  6. Run: mysql -e "CREATE DATABASE testing;"
  7. cd to /app
  8. Run: curl -o setup-phpunit.sh https://raw.githubusercontent.com/afragen/setup-phpunit/master/setup-phpunit.sh
  9. Run: bash setup-phpunit.sh

Thanks @afragen and @clay.

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.

1 Like

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.

1 Like

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 :slight_smile:

Now I get the correct output.

1 Like