Expectations

a minimalist's unit testing framework

This project is maintained by seancorfield and jaycfields

expectations

adding signal, removing noise

Installing

The easiest way to use expectations in your own projects is via Leiningen or Boot. Add the following dependency to your project.clj or build.boot file:

[expectations "2.2.0-beta1"]

note: expectations can go in a :dev profile in project.clj, or have :scope "test" in build.boot, to avoid pushing the dependency downstream.

To build expectations from source, use Leiningen and run the following commands:

$ lein clean
$ lein javac
$ lein jar

Getting Started With Leiningen

Add simple_test.clj to your test directory

(ns simple-test
  (:require [expectations :refer :all)])

(expect nil? nil)

expectations integrates with Leiningen via lein-expectations.

Usage for lein-expectations:

Declare lein-expectations in project.clj:

:plugins [[lein-expectations "0.0.8"]]

To run all your tests:

$ lein expectations

You can also use lein-autoexpect to automatically run expectations when your Clojure source changes.

Alternatively, as of version 2.2.0-alpha1, you can use lein test and write your tests using the clojure.test compatibility style of expectations. When using this style you can use lein-test-refresh to automatically run expectations when your Clojure source changes.

Getting Started With Boot

Add simple_test.clj to your test directory

(ns simple-test
  (:require [expectations :require :all)])

(expect nil? nil)

expectations integrates with Boot via boot-expectations.

Usage for boot-expectations:

Declare boot-expectations in build.boot:

(merge-env! :dependencies
    '[[seancorfield/boot-expectations "1.0.11" :scope "test"]])
(require '[seancorfield.boot-expectations :refer [expectations]])

To run all your tests:

$ boot expectations

You can also use boot watch expectations to automatically run expectations when your Clojure source changes.

Alternatively, as of version 2.2.0-alpha1, you can use boot test, and the boot-test task, and write your tests using the clojure.test compatibility style of expectations.

Getting Started With Emacs

Add simple_test.clj to your test directory

(ns simple-test
  (:require [expectations :require :all)])

(expect nil? nil)

Follow the directions available on expectations-mode

Alternatively, as of version 2.2.0-alpha1, you can use CIDER's standard test running commands, if you write your tests using the clojure.test compatibility style of expectations.

Getting Started With IntelliJ

Create a directory and mark it as a Test Source. more info For example, you can create proj/test/java and mark 'java' as a test source. Then you also create proj/test/clojure and also mark that as a test source directory.

If you want all of your tests to run in JUnit all you need to do is implement ExpectationsTestRunner.TestSource. The following example is what I use to run all the tests in expectations with JUnit.

import expectations.junit.ExpectationsTestRunner;
import org.junit.runner.RunWith;

@RunWith(expectations.junit.ExpectationsTestRunner.class)
public class ClojureTests implements ExpectationsTestRunner.TestSource{

    public String testPath() {
        // return the path to your root test dir here
        return "test/root/dir";
    }
}

Create ClojureTests.java in your proj/test/java directory. Next you can create sample-test in proj/test/clojure.

(ns simple-test
  (:require [expectations :require :all)])

(expect nil? nil)

That's it, you can now use the JUnit Test Runner built into IntelliJ to execute your expectations.

Getting Started Otherwise

By default the tests run on JVM shutdown, so all you need to do is run your clj file and you should see the expectations output.

You can test that everything is working correctly by using expectations in a simple test.

(ns simple.test
  (:require [expectations :require :all)])

(expect nil? nil)

(assuming you've put your dependencies in a (relatively pathed) lib dir)

Running your clj should be similar to:

$ java -cp "lib/*" clojure.main -i /path/to/your/simple/test.clj

At this point you should see output similar to:

Ran 1 tests containing 1 assertions in 5 msecs
0 failures, 0 errors.

You can run the examples in expectations with:

$ java -cp "lib/*" clojure.main -i /path/to/expectations/test/clojure/success/success_examples.clj