1

When running a janet test suite with the jpm command there is no listing of test cases.

$ jpm test

running test/test.janet ...

Failed: (= "hello" "holla")
Expected: "hello"
Actual: "holla"
Test: this should faild


Tests: 1/3 passed
Duration: 0.000 seconds

What I am looking for is to have an output similar to the one that you see when you run go test -v or pytest -v.

oz123
  • 27,559
  • 27
  • 125
  • 187

1 Answers1

1

This is possible if one uses testament:
With testament the tests are written as:

(import testament :prefix "" :exit true)

(deftest one-plus-one
  (is (= 2 (+ 1 1)) "1 + 1 = 2"))

(deftest two-plus-two
  (is (= 5 (+ 2 2)) "2 + 2 = 5"))

(deftest test-should-fail
  (is (= "hello" "holla") "should fail"))

Each test case is defined as:

(deftest <testname> (is <assertion>) <note>)

Testament allows defining a function which will be called after each test:

(set-on-result-hook reporter)
(run-tests!)

In this case, reporter can be defined as:

(defn reporter
  "pretty report"
  [result]
  (var symbol "\e[31m✕\e[0m")
  (if (result :passed?) (set symbol "\e[32m✓\e[0m"))
  (print symbol " " (result :note))
  )

The result will be:

$ jpm test
running test/test.janet ...
✓ 1 + 1 = 2
✕ should fail
✕ 2 + 2 = 5

> Failed: two-plus-two
Assertion: 2 + 2 = 5
Expect: 5
Actual: 4

> Failed: test-should-fail
Assertion: should fail
Expect: "hello"
Actual: "holla"

-----------------------------------
3 tests run containing 3 assertions
1 tests passed, 2 tests failed
-----------------------------------

In a Linux terminal the x will be red, and will be green.

The complete test code:

$ cat test/testam.janet 

(import testament :prefix "" :exit true)

(deftest one-plus-one
  (is (= 2 (+ 1 1)) "1 + 1 = 2"))

(deftest two-plus-two
  (is (= 5 (+ 2 2)) "2 + 2 = 5"))

(deftest test-should-fail
  (is (= "hello" "holla") "should fail"))

(defn reporter
  "pretty report"
  [result]
  (var symbol "\e[31m✕\e[0m")
  (if (result :passed?) (set symbol "\e[32m✓\e[0m"))
  (print symbol " " (result :note))
  )


(set-on-result-hook reporter)
(run-tests!)
oz123
  • 27,559
  • 27
  • 125
  • 187