CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-clojure--clojure

Clojure core environment and runtime library providing a dynamic, general-purpose programming language on the JVM.

Pending
Overview
Eval results
Files

testing.mddocs/

Testing Framework

Comprehensive unit testing framework through clojure.test namespace. Clojure provides a built-in testing framework with assertions, fixtures, and test runners.

Capabilities

Test Definition

Core macros for defining tests and organizing test code.

(deftest name & body)
;; Macro: defines a test function
;; Test function has no parameters and runs assertions
;; Returns: clojure.lang.Var

(testing string & body)
;; Macro: groups assertions with descriptive string
;; Provides context for test failures
;; Returns: result of last body expression

(with-test definition & tests)
;; Macro: defines function/var with embedded tests
;; tests are run when namespace is tested
;; Returns: defined var

Assertions

Assertion macros for testing conditions and values.

(is expr)
(is expr msg)
;; Macro: generic assertion - tests that expr is truthy
;; Optional custom failure message
;; Returns: expr result or throws on failure

(is (= expected actual))
;; Common pattern: test equality
(is (= expected actual) "Values should be equal")

(is (thrown? ExceptionClass expr))
;; Tests that expr throws specific exception type
(is (thrown? ArithmeticException (/ 1 0)))

(is (thrown-with-msg? ExceptionClass regex expr))
;; Tests exception type and message pattern
(is (thrown-with-msg? Exception #"divide" (/ 1 0)))

(are argv expr & args)
;; Macro: template for multiple similar assertions
;; argv: parameter vector, expr: template expression
;; args: argument sets to substitute
;; Returns: results of all instantiated expressions

Test Execution

Functions for running tests and collecting results.

(run-tests & namespaces)
;; Function: runs all tests in specified namespaces
;; If no namespaces given, runs current namespace
;; Returns: test result summary map

(run-all-tests)
(run-all-tests re)
;; Function: runs tests in all loaded namespaces
;; Optional regex to filter namespace names
;; Returns: test result summary map

(test-ns ns)
;; Function: runs all tests in specific namespace
;; Returns: test result summary map

(test-all-vars ns)
;; Function: runs all test vars in namespace
;; Returns: test result summary map

(test-var v)
;; Function: runs single test var
;; Returns: test result summary map

(test-vars vars)
;; Function: runs collection of test vars
;; Returns: test result summary map

Test Results and Reporting

Functions for handling test results and customizing output.

(successful? summary)
;; Function: returns true if test summary shows all tests passed
;; summary: result map from run-tests functions
;; Returns: boolean

(do-report m)
;; Function: processes test event for reporting
;; m: map with :type and test details
;; Multimethod - can be extended for custom reporting
;; Returns: nil

;; Test event types:
;; :begin-test-ns, :end-test-ns - namespace start/end
;; :begin-test-var, :end-test-var - test var start/end  
;; :pass - assertion passed
;; :fail - assertion failed
;; :error - exception during test
;; :summary - final results

Fixtures

Setup and teardown functions that wrap test execution.

(use-fixtures fixture-type & fixtures)
;; Function: installs fixtures for current namespace
;; fixture-type: :once (per namespace) or :each (per test)
;; fixtures: functions that take and call a test function
;; Returns: nil

(compose-fixtures f1 f2)
;; Function: composes two fixture functions
;; Returns: composed fixture function

(join-fixtures fixtures)
;; Function: composes collection of fixtures  
;; Returns: composed fixture function

Test Utilities

Helper functions and variables for test development.

*testing-vars*
;; Dynamic var: stack of vars currently being tested
;; Type: list of clojure.lang.Var

*testing-contexts*  
;; Dynamic var: stack of testing context strings
;; Type: list of String

*test-out*
;; Dynamic var: PrintWriter for test output
;; Type: java.io.PrintWriter

(function? x)
;; Function: returns true if x is a function or callable
;; More comprehensive than fn?
;; Returns: boolean

(testing-vars-str m)
;; Function: formats current testing vars as string
;; Returns: String

(testing-contexts-str contexts)
;; Function: formats testing contexts as string
;; Returns: String

(inc-report-counter name)
;; Function: increments named counter in test results
;; name: :test, :pass, :fail, :error
;; Returns: new count

(file-position)
;; Function: returns current file and line info
;; Returns: map with :file and :line

Custom Assertions

Building custom assertion functions and predicates.

(assert-predicate msg pred expected actual)
;; Function: creates assertion using predicate function
;; Returns: actual value or reports failure

(assert-any msg expected actual)
;; Function: generic assertion with custom reporting
;; Returns: actual value or reports failure

;; Example custom assertion:
(defn is-even [x]
  (is (even? x) (str x " should be even")))

Usage Examples:

;; Basic test definition
(deftest simple-math-test
  (is (= 4 (+ 2 2)))
  (is (= 0 (- 5 5)))
  (is (not= 4 (+ 2 3))))

;; Testing with context
(deftest string-operations-test
  (testing "String concatenation"
    (is (= "hello world" (str "hello" " " "world"))))
  (testing "String length"
    (is (= 5 (.length "hello"))))
  (testing "String comparison"
    (is (.startsWith "hello world" "hello"))))

;; Template testing with are
(deftest arithmetic-test
  (are [x y expected] (= expected (+ x y))
    1 2 3
    0 0 0
    -1 1 0
    10 -5 5))

;; Exception testing
(deftest exception-test
  (is (thrown? ArithmeticException (/ 1 0)))
  (is (thrown-with-msg? NumberFormatException 
                        #"For input string"
                        (Integer/parseInt "not-a-number"))))

;; Test fixtures
(defn database-fixture [test-fn]
  (setup-test-database)
  (try
    (test-fn)
    (finally
      (cleanup-test-database))))

(use-fixtures :each database-fixture)

;; Multiple fixtures
(defn logging-fixture [test-fn]
  (println "Starting test")
  (test-fn)
  (println "Finished test"))

(use-fixtures :each database-fixture logging-fixture)

;; Running tests
(run-tests)                           ; Run current namespace tests
(run-tests 'my.namespace)             ; Run specific namespace
(run-all-tests)                       ; Run all loaded namespaces
(run-all-tests #"my\.project\..*")    ; Run namespaces matching regex

;; Test results
(def results (run-tests))
(successful? results)                 ; => true/false
(:test results)                       ; Number of tests run
(:pass results)                       ; Number of assertions passed
(:fail results)                       ; Number of assertions failed
(:error results)                      ; Number of errors

;; Custom reporting
(defmethod do-report :fail [m]
  (println "FAILURE in" (testing-vars-str m))
  (when (seq *testing-contexts*)
    (println (testing-contexts-str *testing-contexts*)))
  (println "Expected:" (pr-str (:expected m)))
  (println "Actual:" (pr-str (:actual m))))

;; Property-based testing pattern
(deftest property-test
  (dotimes [_ 100]
    (let [x (rand-int 1000)
          y (rand-int 1000)]
      (is (= (+ x y) (+ y x)) "Addition should be commutative"))))

;; Integration with fixtures
(defn with-temp-file [test-fn]
  (let [temp-file (java.io.File/createTempFile "test" ".tmp")]
    (try
      (binding [*temp-file* temp-file]
        (test-fn))
      (finally
        (.delete temp-file)))))

;; Embedded tests with with-test
(with-test
  (defn multiply [x y]
    (* x y))
  
  (is (= 6 (multiply 2 3)))
  (is (= 0 (multiply 0 5)))
  (is (= -10 (multiply -2 5))))

Install with Tessl CLI

npx tessl i tessl/maven-org-clojure--clojure

docs

concurrency.md

control-flow.md

core-language.md

data-structures.md

data-utilities.md

dev-tools.md

formats.md

index.md

io.md

java-interop.md

math.md

sequences.md

strings.md

testing.md

tile.json