Clojure Leiningen その5

テスト

  • lein test コマンドでテストを実行。
  • 例。
vagrant@ubuntu2204:~/source/temp$ lein test

lein test temp.core-test

lein test :only temp.core-test/a-test

FAIL in (a-test) (core_test.clj:7)
FIXME, I fail.
expected: (= 0 1)
  actual: (not (= 0 1))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Subprocess failed (exit code: 1)
  • ↑ テストが一件落ちていると警告が出ている。
  • ↑ この場合、テストが少ないので問題ないが、テストが増えてくると時間がかかるようになってくる。名前空間を指定すれば、そこだけテストを実行できる。
  • 例。
vagrant@ubuntu2204:~/source/temp$ lein test temp.core-test

lein test temp.core-test

lein test :only temp.core-test/a-test

FAIL in (a-test) (core_test.clj:7)
FIXME, I fail.
expected: (= 0 1)
  actual: (not (= 0 1))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Subprocess failed (exit code: 1)
  • 存在しない名前空間を指定したらどうなるんだろう?
vagrant@ubuntu2204:~/source/temp$ lein test temp.core

lein test user

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
  • エラーは出ないので注意。代わりに(?) Ran 0 tests とメッセージが出る。
  • REPLで clojure.test/run-tests を使ってもテストを実行できる。この場合、JVMをいちいち起動しないので高速にテストを実行できる。
vagrant@ubuntu2204:~/source/temp$ lein repl
nREPL server started on port 38937 on host 127.0.0.1 - nrepl://127.0.0.1:38937
REPL-y 0.5.1, nREPL 0.9.0
Clojure 1.11.1
OpenJDK 64-Bit Server VM 17.0.5+8-LTS
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

temp.core=> (use 'clojure.test)
nil
temp.core=> (clojure.test/run-tests)

Testing temp.core

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
{:test 0, :pass 0, :fail 0, :error 0, :type :summary}
temp.core=> 
  • Ran 0 tests と言われた。
  • よく見ると実行しているテストの名前空間がテストの名前空間ではない。
  • 名前空間を指定するとうまく実行できた。どうやらデフォルトではREPLで今いる名前空間のテストを実行するみたい。
temp.core=> (require 'temp.core-test)
nil
temp.core=> (clojure.test/run-tests 'temp.core-test)

Testing temp.core-test

FAIL in (a-test) (core_test.clj:7)
FIXME, I fail.
expected: (= 0 1)
  actual: (not (= 0 1))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:test 1, :pass 0, :fail 1, :error 0, :type :summary}
temp.core=> 
  • run-tests は繰り返し実行する時間の短縮にはなるが、テストコードを変更した場合(またはその他プロダクションコードを変更した場合も)REPLの世界に変更を取り込まないと反映されない。そのため、変更の取り込みを簡単に行う仕組みを別途整える必要がある。