I'm following https://make.wordpress.org/cli/handbook/plugin-unit-tests/ to run unit tests for plugins.

I create a new plugin.

$ wp scaffold plugin hello-plugin-1
Success: Created plugin files.
Success: Created test files.

Run install-wp-tests.sh.

$ cd wp-content/plugins/hello-plugin-1
$ ./bin/install-wp-tests.sh wordpress_test_1 root root localhost latest
+ install_wp
+ '[' -d /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/ ']'
+ return
+ install_test_suite
++ uname -s
+ [[ Darwin == \D\a\r\w\i\n ]]
+ local ioption=-i.bak
+ '[' '!' -d /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib ']'
+ '[' '!' -f wp-tests-config.php ']'
+ download https://develop.svn.wordpress.org/tags/5.4/wp-tests-config-sample.php /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
++ which curl
+ '[' /usr/bin/curl ']'
+ curl -s https://develop.svn.wordpress.org/tags/5.4/wp-tests-config-sample.php
++ echo /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/
++ sed 's:/\+$::'
+ WP_CORE_DIR=/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/
+ sed -i.bak 's:dirname( __FILE__ ) . '\''/src/'\'':'\''/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress//'\'':' /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/youremptytestdbnamehere/wordpress_test_1/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/yourusernamehere/root/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/yourpasswordhere/root/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak 's|localhost|localhost|' /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ install_db
+ '[' false = true ']'
+ PARTS=(${DB_HOST//\:/ })
+ local PARTS
+ local DB_HOSTNAME=localhost
+ local DB_SOCK_OR_PORT=
+ local EXTRA=
+ '[' -z localhost ']'
++ echo
++ grep -e '^[0-9]\{1,\}$'
+ '[' ']'
+ '[' -z ']'
+ '[' -z localhost ']'
+ EXTRA=' --host=localhost --protocol=tcp'
+ mysqladmin create wordpress_test_1 --user=root --password=root --host=localhost --protocol=tcp
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

Run phpunit and got this DB connection error.

$ phpunit
PHP Warning:  mysqli_real_connect(): (HY000/2002): No such file or directory in /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php on line 1626
PHP Stack trace:
PHP   1. {main}() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/includes/install.php:0
PHP   2. require_once() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/includes/install.php:29
PHP   3. require_wp_db() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-settings.php:126
PHP   4. wpdb->__construct() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/load.php:426
PHP   5. wpdb->db_connect() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php:631
PHP   6. mysqli_real_connect() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php:1626

wp_die called
Message : <p><code>No such file or directory</code></p>
<h1>Error establishing a database connection</h1>
<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can&#8217;t contact the database server at <code>localhost</code>. This could mean your host&#8217;s database server is down.</p>
<li>Are you sure you have the correct username and password?</li>
<li>Are you sure you have typed the correct hostname?</li>
<li>Are you sure the database server is running?</li>
<p>If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="https://wordpress.org/support/forums/">WordPress Support Forums</a>.</p>

Title : 

Do we need to set up the DB in wp-config.php in /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress before I run phpunit? I tried it and it did not work. Any idea what went wrong?

I'm running WP-CLI 2.4.0 and PHPUnit 7.5.9.


I fixed the issue by changing localhost to in /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.