Unit testing &引用;lein测试:仅foo.bar.test/testme“;“不拾取功能”;“测试我”;如果;测试ns挂钩“;定义

Unit testing &引用;lein测试:仅foo.bar.test/testme“;“不拾取功能”;“测试我”;如果;测试ns挂钩“;定义,unit-testing,clojure,leiningen,Unit Testing,Clojure,Leiningen,我已经用测试函数定义了Clojure名称空间,并希望通过lein test使用Leiningen 2.9.1运行它们 测试函数按层次进行组织。如果我只是运行lein test,所有deftest都将被拾取,从而导致测试重复。例如: (ns foo.bar.test (:require [clojure.test :as t] [clojure.spec.alpha :as s] [foo.bar.main :as su

我已经用测试函数定义了Clojure名称空间,并希望通过
lein test
使用Leiningen 2.9.1运行它们

测试函数按层次进行组织。如果我只是运行
lein test
,所有
deftest
都将被拾取,从而导致测试重复。例如:

(ns foo.bar.test
   (:require
      [clojure.test         :as t]
      [clojure.spec.alpha   :as s]
      [foo.bar.main         :as sut])) ; system under test

(t/deftest test-strip-empty
   (t/is
      (s/valid? ::sut/a-spec some-value)))

(t/deftest test-strip-several-squares
   (t/is
      (s/valid? ::sut/a-spec some-value)))

; collect subtests

(t/deftest testcollect-strip
   (test-strip-empty)
   (test-strip-several-squares))
lein test
将运行所有三个
deftest
条目,从而运行
测试条清空
测试条数个方块
两次

函数
test ns hook
可以定义为显式调用“测试树的顶部”

如果存在,
lein test
将只调用
test ns hook

太好了

但一旦它存在,我就不能再运行单独的测试了

不好

删除
test ns hook
的定义,它可以工作:


将这两种功能结合起来是否可以最大限度地提高幸福感:保留定义的
test ns hook
并能够运行单独的测试?

不要像使用
testcollect strip
那样对测试进行分组。我称之为反模式

您可以使用
testing
宏在单个
deftest
表单中对单个断言进行分层:


您还可以使用测试选择器仅运行测试的子集:

因此,将元数据添加到测试定义中

(deftest ^:basic-math t-math
  (testing "Arithmetic"
    (testing "with positive integers"
      (is (= 4 (+ 2 2)))
      (is (= 7 (+ 3 4))))
    (testing "with negative integers"
      (is (= -4 (+ -2 -2)))
      (is (= -1 (+ 3 -4))))))
并声明测试选择器
:basics
,以获取
项目中标记有
:basic math
的所有内容。clj

(defproject foo.bar "0.1.0-SNAPSHOT"
  ...
  :test-selectors {:basics :basic-math})
现在只能通过以下方式运行标有
:basic math
的测试:

还有一个诀窍需要记住。测试代码(dirs/文件)的命名空间结构不需要与源代码的命名空间结构匹配。您可以有一个单一的源代码ns
super.calc
,但有一个完整的测试命名空间层次结构。我用根
tst.
前缀作为它们的前缀,我认为这比在所有东西上都加上
\u test
后缀带来了更好的命名结构:

tst.super.calc
tst.super.calc.add
tst.super.calc.add.int
tst.super.calc.add.int.pos
tst.super.calc.add.int.neg
tst.super.calc.add.float
tst.super.calc.add.float.pos
tst.super.calc.add.float.neg
tst.super.calc.mult
...
所以你可以得到你想要的细粒度。将其与lein测试选择器混合使用,可以实现几乎无限细粒度的控制


另外,

请检查lein测试刷新,这是我最喜欢的lein测试方式


是否要隔离测试?使用选择器可能会更容易。@cfrick我找到了谢谢。有很多信息需要解包。
(deftest t-math
  (testing "Arithmetic"
    (testing "with positive integers"
      (is (= 4 (+ 2 2)))
      (is (= 7 (+ 3 4))))
    (testing "with negative integers"
      (is (= -4 (+ -2 -2)))
      (is (= -1 (+ 3 -4))))))
~/expr/demo > lein clean ; lein test

lein test _bootstrap

-------------------------------
   Clojure 1.10.0    Java 12
-------------------------------

lein test tst.demo.core

Ran 2 tests containing 4 assertions.
0 failures, 0 errors.
~ > lein help test
Run the project's tests.

Marking deftest or ns forms with metadata allows you to pick selectors to
specify a subset of your test suite to run:

    (deftest ^:integration network-heavy-test
      (is (= [1 2 3] (:numbers (network-operation)))))

Write the selectors in project.clj:

    :test-selectors {:default (complement :integration)
                     :integration :integration}

Arguments to this task will be considered test selectors if they are keywords,
otherwise arguments must be test namespaces or files to run. With no
arguments the :default test selector is used if present, otherwise all
tests are run. Test selector arguments must come after the list of namespaces.

A default :only test-selector is available to run select tests. For example,
`lein test :only leiningen.test.test/test-default-selector` only runs the
specified test. A default :all test-selector is available to run all tests.

Arguments: ([& tests])
(deftest ^:basic-math t-math
  (testing "Arithmetic"
    (testing "with positive integers"
      (is (= 4 (+ 2 2)))
      (is (= 7 (+ 3 4))))
    (testing "with negative integers"
      (is (= -4 (+ -2 -2)))
      (is (= -1 (+ 3 -4))))))
(defproject foo.bar "0.1.0-SNAPSHOT"
  ...
  :test-selectors {:basics :basic-math})
~ > lein test :basics
tst.super.calc
tst.super.calc.add
tst.super.calc.add.int
tst.super.calc.add.int.pos
tst.super.calc.add.int.neg
tst.super.calc.add.float
tst.super.calc.add.float.pos
tst.super.calc.add.float.neg
tst.super.calc.mult
...