• Stars
    star
    152
  • Rank 243,924 (Top 5 %)
  • Language
    JavaScript
  • Created over 9 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Containerized unit testing across any platform and programming language

Dockunit

Containerized unit testing across any platform and programming language.

Purpose

We all want to test our applications on as many relevant platforms as possible. Sometimes this is easy. Sometimes it's not. Dockunit let's you define a set of Docker containers to run your tests against. You can run your test framework of choice in your language of choice on any type of environment. In the past many developers, myself included, have relied on Travis CI to run tests in environments that aren't setup locally (i.e. PHP 5.2). With Dockunit you don't need to do this anymore.

Requirements

  • OSX or a Linux Distribution (Windows not yet tested or officially supported)
  • Node.js
  • npm
  • Docker

Installation

  1. Make sure you have Node.js, Docker, and npm install
  2. Install via npm:
npm install -g dockunit

Usage

Dockunit relies on Dockunit.json files. Each of your projects should have their own Dockunit.json file. Dockunit.json defines what test commands should be run on what type of containers for any given project. Here is an example Dockunit.json:

{
  "containers": [
    {
      "prettyName": "PHP 5.2 on Ubuntu",
      "image": "user/my-php-image",
      "beforeScripts": [],
      "testCommand": "phpunit"
    },
    {
      "prettyName": "PHP 5.6 FPM on Ubuntu",
      "image": "user/my-php-image2",
      "beforeScripts": [],
      "testCommand": "phpunit"
    }
  ]
}

containers contains an array of container objects. Each container object can contain the following properties:

  • prettyName (required) - This is used in output to help you identify your container.
  • image (required) - This is a valid Docker container image located in the Docker registry. We have a number of handy prebuilt Docker images for use in your Dockunit.json files.
  • beforeScripts (optional) - This is a string array of bash scripts to be run in order.
  • testCommand (required) - This is the actual test command to be run on each container i.e. phpunit or qunit.

The Dockunit command is:

dockunit <path-to-project-directory> [--du-verbose] [--du-container] [--help] [--version] ...

Note: sudo is usually required when run within a Linux distribution since Dockunit runs Docker commands which require special permissions.

  • <path-to-project-directory> (optional) - If you run dockunit in a folder with a Dockunit.json folder, it will detect it automatically.
  • [--du-verbose] (optional) - This will print out light verbose Dockunit output. [--du-verbose=2] will output even more verbose Dockunit output.
  • [--du-container] (optional) - Run only one container in your Dockunit.json file by specifying the index of that container in the containers array .i.e --du-container=1.
  • [--help] (optional) - This will display usage information for the dockunit command.
  • [--version] (optional) - This will display the current installed version of Dockunit.
  • ... - Any additional arguments and options passed to the command will be passed to your test command. For example, if you wanted to pass a few extra options to PHPUnit, you could append them to the end of your dockunit command.

You can simply run dockunit in any folder with a Dockunit.json to run Dockunit.

Dockunit.json Examples

Each of your projects should have a Dockunit.json file in the project root. You should define your containers to fit your application's unique needs. Here's a few example Dockunit.json files for a variety of different programming languages and environments. Feel free to use any of our prebuilt Docker images in your Dockunit.json files or create your own.

PHP and WordPress

Dockunit and WordPress work well together. WordPress is backwards compatible with PHP 5.2. It's very difficult to test applications on PHP 5.2 without some sort of containerized workflow. Here is an example Dockunit.json file that you can use to test your WordPress plugins in PHP 5.2, 5.6, and PHP 7.0 RC 1 (make sure to replace PLUGIN-FILE.php with your plugins main file):

{
  "containers": [
    {
      "prettyName": "PHP-FPM 5.2 WordPress Latest",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-wordpress-5.2-fpm",
      "beforeScripts": [
        "service mysql start",
        "wp-install latest"
      ],
      "testCommand": "wp-activate-plugin PLUGIN-FILE.php"
    },
    {
      "prettyName": "PHP-FPM 5.6 WordPress Latest",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-wordpress-5.6-fpm",
      "beforeScripts": [
        "service mysql start",
        "wp core download --path=/temp/wp --allow-root",
        "wp core config --path=/temp/wp --dbname=test --dbuser=root --allow-root",
        "wp core install --url=http://localhost --title=Test --admin_user=admin --admin_password=12345 [email protected] --path=/temp/wp --allow-root",
        "mkdir /temp/wp/wp-content/plugins/test",
        "cp -r . /temp/wp/wp-content/plugins/test"
      ],
      "testCommand": "wp plugin activate test --allow-root --path=/temp/wp"
    },
    {
      "prettyName": "PHP-FPM 7.0 WordPress Latest",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-wordpress-7.0-rc-1-fpm",
      "beforeScripts": [
        "service mysql start",
        "wp core download --path=/temp/wp --allow-root",
        "wp core config --path=/temp/wp --dbname=test --dbuser=root --allow-root",
        "wp core install --url=http://localhost --title=Test --admin_user=admin --admin_password=12345 [email protected] --path=/temp/wp --allow-root",
        "mkdir /temp/wp/wp-content/plugins/test",
        "cp -r . /temp/wp/wp-content/plugins/test"
      ],
      "testCommand": "wp plugin activate test --allow-root --path=/temp/wp"
    }
  ]
}

Here is an example Dockunit.json file that you can use to test your WordPress themes in PHP 5.2, 5.6, and PHP 7.0 RC 1:

{
  "containers": [
    {
      "prettyName": "PHP-FPM 5.2 WordPress Latest",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-wordpress-5.2-fpm",
      "beforeScripts": [
        "service mysql start",
        "wp-install latest"
      ],
      "testCommand": "wp-activate-theme test"
    },
    {
      "prettyName": "PHP-FPM 5.6 WordPress Latest",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-wordpress-5.6-fpm",
      "beforeScripts": [
        "service mysql start",
        "wp core download --path=/temp/wp --allow-root",
        "wp core config --path=/temp/wp --dbname=test --dbuser=root --allow-root",
        "wp core install --url=http://localhost --title=Test --admin_user=admin --admin_password=12345 [email protected] --path=/temp/wp --allow-root",
        "mkdir /temp/wp/wp-content/themes/test",
        "cp -r . /temp/wp/wp-content/themes/test"
      ],
      "testCommand": "wp theme activate test --allow-root --path=/temp/wp"
    },
    {
      "prettyName": "PHP-FPM 7.0 WordPress Latest",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-wordpress-7.0-rc-1-fpm",
      "beforeScripts": [
        "service mysql start",
        "wp core download --path=/temp/wp --allow-root",
        "wp core config --path=/temp/wp --dbname=test --dbuser=root --allow-root",
        "wp core install --url=http://localhost --title=Test --admin_user=admin --admin_password=12345 [email protected] --path=/temp/wp --allow-root",
        "mkdir /temp/wp/wp-content/themes/test",
        "cp -r . /temp/wp/wp-content/themes/test"
      ],
      "testCommand": "wp theme activate test --allow-root --path=/temp/wp"
    }
  ]
}

PHP and WordPress Unit Tests

Here are some more advanced WordPress examples. That assume you have unit tests setup via WP-CLI.

{
  "containers": [
    {
      "prettyName": "PHP 5.2 FPM WordPress 4.1",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-5.2-fpm",
      "beforeScripts": [
        "service mysql start",
        "bash bin/install-wp-tests.sh wordpress_test root '' localhost 4.1"
      ],
      "testCommand": "phpunit"
    },
    {
      "prettyName": "PHP 5.6 FPM WordPress 4.0",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-5.6-fpm",
      "beforeScripts": [
        "service mysql start",
        "bash bin/install-wp-tests.sh wordpress_test2 root '' localhost 4.0"
      ],
      "testCommand": "phpunit"
    },
    {
      "prettyName": "PHP 7.0 RC-1",
      "image": "dockunit/prebuilt-images:php-mysql-phpunit-7.0-rc-1-fpm",
      "beforeScripts": [
        "service mysql start",
        "bash bin/install-wp-tests.sh wordpress_test3 root '' localhost 3.9"
      ],
      "testCommand": "phpunit"
    }
  ]
}

dockunit/prebuilt-images:php-mysql-phpunit-5.6-fpm, dockunit/prebuilt-images:php-mysql-phpunit-5.6-fpm, and dockunit/prebuilt-images:php-mysql-phpunit-7.0-rc-1-fpm are valid Docker images available for use in any Dockerfile.json.

Node.js

It is super easy to test your Node.js applications with Dockunit. Here is a simple Dockunit.json file that tests an application in Node.js 0.10.x and 0.12.0 using mocha:

{
  "containers": [
    {
      "prettyName": "Node 0.10.x",
      "image": "google/nodejs:latest",
      "beforeScripts": [
        "npm install -g mocha"
      ],
      "testCommand": "mocha"
    },
    {
      "prettyName": "Node 0.12",
      "image": "tlovett1/nodejs:0.12",
      "beforeScripts": [
        "npm install -g mocha"
      ],
      "testCommand": "mocha"
    }
  ]
}

google/nodejs is a valid Docker image available for use in any Dockerfile.json.

Python

Dockunit works great with Python. This Dockunit.json file tests in Python 2.7.9 and the latest Python version using nose:

{
  "containers": [
    {
      "prettyName": "Python 2.7.9",
      "image": "python:2.7.9",
      "beforeScripts": [
        "easy_install nose"
      ],
      "testCommand": "nosetests tests"
    },
    {
      "prettyName": "Python Latest",
      "image": "python:latest",
      "beforeScripts": [
        "easy_install nose"
      ],
      "testCommand": "nosetests tests"
    }
  ]
}

Ruby

You can use Dockunit to test your Ruby scripts. This Dockunit.json file tests a project in Ruby 2.1 and the latest stable Ruby version using test-unit:

{
  "containers": [
    {
      "prettyName": "Latest version of Ruby",
      "image": "ruby:latest",
      "beforeScripts": [
        "bundle install"
      ],
      "testCommand": "bundle exec rake test"
    },
    {
      "prettyName": "Ruby version 2.1",
      "image": "ruby:2.1",
      "beforeScripts": [
        "bundle install"
      ],
      "testCommand": "bundle exec rake test"
    }
  ]
}

ruby is a valid Docker image available for use in any Dockerfile.json.

License

Dockunit is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

More Repositories

1

_s_backbone

A WordPress starter theme built off _s and the JSON REST API Backbone client.
PHP
189
star
2

custom-contact-forms

Build beautiful custom forms and manage submissions the WordPress way.
PHP
161
star
3

simple-cache

A simple caching plugin for WordPress.
PHP
130
star
4

json-rest-api-subscriptions

Webhooks style subscriptions for all post types via the WordPress JSON REST API.
PHP
53
star
5

alexa-skill-test

Test Alexa skills through a local Express server.
JavaScript
43
star
6

wordpress-ansible-playbook

Simple playbook for setting up WordPress with MariaDB, PHP FPM, nginx, and Memcache
PHP
41
star
7

feed-pull

Pull feeds into WordPress
PHP
29
star
8

editorial-access-manager

Granular editorial access control for all post types in WordPress
PHP
18
star
9

wp-media-pro

WP Media Pro is the must-have toolkit for all WordPress websites that seriously use media.
PHP
11
star
10

detect-wp-core-modifications

A node command that checks WordPress for core file modifications
JavaScript
7
star
11

brotli-nginx-php7-config

Simple PHP7 FPM nginx configuration file using Brotli with gzip as a fallback.
Nginx
6
star
12

repo-voice

An Alexa skill to talk to GitHub.
JavaScript
4
star
13

wp-cron-node

Run WordPress cron with Node.js and WP-CLI
JavaScript
3
star
14

burnthouse

Burnthouse WP theme
JavaScript
2
star
15

dynamic-cloudinary

Automatically serve all your images optimized from the cloud.
PHP
2
star
16

redis-object-cache

Redis object cache drop-in for WordPress
PHP
2
star
17

post-format-options

WordPress plugin that allows administrators to granularly manage post format options.
PHP
1
star
18

tlcom-theme

Taylorlovett.com Theme
CSS
1
star
19

tennis-facts-alexa-skill

Ask Alexa for Tennis Facts
JavaScript
1
star
20

fluxible-starter

A Fluxible starter application.
JavaScript
1
star