I am writing some unit tests that interact with a database. For this reason it is useful to have a setup and a teardown method in my unit test to create and then drop the table. However there are no docs :O on the use-fixtures method.
Here is what i need to do:
(setup-tests)
(run-tests)
(teardown-tests)
I am not interested currently in running a setup and teardown before and after each test, but once before a group of tests and once after. How do you do this?
Test fixtures can be set up three different ways: in-line, delegate, and implicit. In-line setup creates the test fixture in the same method as the rest of the test. While in-line setup is the simplest test fixture to create, it leads to duplication when multiple tests require the same initial data.
Test Fixtures: Using the Same Data Configuration for Multiple Tests If you find yourself writing two or more tests that operate on similar data, you can use a test fixture. This allows you to reuse the same configuration of objects for several different tests.
Running Tests You can run tests by using the cljs. test/run-tests macro. This may be done in your REPL or at the end of your file. If you have many test namespaces it's idiomatic to create a test runner namespace which imports all of your test namespaces and then invokes run-tests .
In software development, test fixtures (fixtures) is a term used to describe any test data that lives outside of that particular test, and is used to set the application to a known fixed state.
You can't use use-fixtures
to provide setup and teardown code for freely defined groups of tests, but you can use :once
to provide setup and teardown code for each namespace:
;; my/test/config.clj
(ns my.test.config)
(defn wrap-setup
[f]
(println "wrapping setup")
;; note that you generally want to run teardown-tests in a try ...
;; finally construct, but this is just an example
(setup-test)
(f)
(teardown-test))
;; my/package_test.clj
(ns my.package-test
(:use clojure.test
my.test.config))
(use-fixtures :once wrap-setup) ; wrap-setup around the whole namespace of tests.
; use :each to wrap around each individual test
; in this package.
(testing ... )
This approach forces some coupling between setup and teardown code and the packages the tests are in, but generally that's not a huge problem. You can always do your own manual wrapping in testing
sections, see for example the bottom half of this blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With