我不知道如何使用
fiveam
测试函数。我使用 cl-project
设置了一个项目。我的项目名称是my-projects/proj1
:
~/quicklisp% tree local-projects
local-projects
├── dtrace
│ ├── dtrace.asd
│ └── dtrace.lisp
├── my-projects
│ └── proj1
│ ├── #proj1.asd#
│ ├── README.markdown
│ ├── README.org
│ ├── proj1.asd
│ ├── proj1.asd~
│ ├── src
│ │ ├── main.fasl
│ │ ├── main.lisp
│ │ └── main.lisp~
│ └── tests
│ ├── main.fasl
│ ├── main.lisp
│ └── main.lisp~
└── system-index.txt
并且:
~/quicklisp/local-projects% cat system-index.txt
dtrace/dtrace.asd
my-projects/proj1/proj1.asd
这是
proj1/proj1.asd
:
(defsystem "proj1"
:version "0.0.1"
:author "7stud"
:license "any"
:depends-on ()
:components ((:module "src"
:components
((:file "main"))))
:description ""
:in-order-to ((test-op (test-op "proj1/tests"))))
(defsystem "proj1/tests"
:author "7stud"
:license "any"
:depends-on ("proj1"
"fiveam")
:components ((:module "tests"
:components
((:file "main"))))
:description "Test system for proj1"
:perform (test-op (op c) (symbol-call :fiveam '#:run! :proj1)))
proj1/src/main.lisp
:
(defpackage proj1
(:use :cl))
(in-package :proj1)
;; blah blah blah.
(defun anyoddp (number-list)
(cond ((null number-list) nil)
((oddp (first number-list)) t)
(t (anyoddp (rest number-list)))))
proj1/tests/main.lisp
:
(defpackage proj1/tests/main
(:use :cl
:proj1
:fiveam))
(in-package :proj1/tests/main)
;; Note: To run this test file, execute `(asdf:test-system :proj1)' in your Lisp.
;; (deftest test-target-1
;; (testing "should (= 1 1) to be true"
;; (ok (= 1 1))))
(def-suite master-suite
:description "Test my system.")
(def-suite anyoddp-tests
:description "Test anyoddp"
:in master-suite)
(in-suite anyoddp-tests)
(test simple-maths
(is (= 3 (+ 1 1))))
(test anyoddp-with-odds
(let ((result (anyoddp '(1 3 4))))
(is (equal result t))
"True expected but got ~a" result))
我已经尝试了我读过的所有命令排列,试图测试我的 proj1“系统”,但我要么得到一个错误,要么得到
Didn't run anything...huh?
。这是我最新的尝试:
CL-USER> (ql:quickload "fiveam")
To load "fiveam":
Load 1 ASDF system:
fiveam
; Loading "fiveam"
("fiveam")
CL-USER> (asdf:load-system "proj1")
T
CL-USER> (asdf:test-system :proj1)
; compiling file "/Users/7stud/quicklisp/local-projects/my-projects/proj1/tests/main.lisp" (written 20 MAR 2024 10:04:40 AM):
; wrote /Users/7stud/.cache/common-lisp/sbcl-2.4.0-macosx-arm64/Users/7stud/quicklisp/local-projects/my-projects/proj1/tests/main-tmpUSHT8RIL.fasl
; compilation finished in 0:00:00.004
Didn't run anything...huh?; in: ALEXANDRIA:NAMED-LAMBDA PROJ1/TESTS/MAIN::%TEST-ANYODDP-WITH-ODDS
; (PROJ1/TESTS/MAIN::ANYODDP '(1 3 4))
;
; caught STYLE-WARNING:
; undefined function: PROJ1/TESTS/MAIN::ANYODDP
;
; compilation unit finished
; Undefined function:
; PROJ1/TESTS/MAIN::ANYODDP
; caught 1 STYLE-WARNING condition
T
CL-USER>
为什么我会得到
未定义函数:PROJ1/TESTS/MAIN::ANYODDP
???在
proj1/tests/main.lisp
的顶部写着:
(defpackage proj1/tests/main
(:use :cl
:proj1
:fiveam))
use :proj1
不是应该让我调用proj1中定义的函数而不需要包名吗?好的,这是 proj1/tests/main.lisp
,将包名称添加到函数名称中 anyoddp
:
(defpackage proj1/tests/main
(:use :cl
:proj1
:fiveam))
(in-package :proj1/tests/main)
(def-suite master-suite
:description "Test my system.")
(def-suite anyoddp-tests
:description "Test anyoddp"
:in master-suite)
(in-suite anyoddp-tests)
(test simple-maths
(is (= 3 (+ 1 1))))
(test anyoddp-with-odds
(let ((result (proj1::anyoddp '(1 3 4)))) ;; **CHANGE HERE**
(is (equal result t))
"True expected but got ~a" result))
然后:
CL-USER> (ql:quickload "fiveam")
To load "fiveam":
Load 1 ASDF system:
fiveam
; Loading "fiveam"
("fiveam")
CL-USER> (asdf:load-system "proj1")
T
CL-USER> (asdf:test-system :proj1)
; compiling file "/Users/7stud/quicklisp/local-projects/my-projects/proj1/tests/main.lisp" (written 20 MAR 2024 10:33:11 AM):
; wrote /Users/7stud/.cache/common-lisp/sbcl-2.4.0-macosx-arm64/Users/7stud/quicklisp/local-projects/my-projects/proj1/tests/main-tmp7YCPD44Y.fasl
; compilation finished in 0:00:00.004
Didn't run anything...huh?
T
CL-USER>
我还想知道是否有一种方法可以使用
fiveam
来测试不在项目中的文件中的函数。假设我有这个文件:
;; a.lisp
(defun anyoddp (number-list)
(cond ((null number-list) nil)
((oddp (first number-list)) t)
(t (anyoddp (rest number-list)))))
如何向
a.lisp
添加一些测试,然后运行这些测试?
我还想知道是否有一种方法可以使用 Fiveam 来测试不在项目中的文件中的函数。
您可以在任何地方定义测试并通过测试套件或测试名称运行它们。
您还可以将它们放在功能标志后面。
第一种可能性,开发者手动检查的测试:
#+(or)
;; or your own marker in *features*.
(equal t (anyoddp '(1 2 3)))
只需按 C-c C-c 就可以了。
我们可以添加 Fiveam 测试。
(use-package :fiveam)
(def-suite oddtest)
(in-suite oddtest)
(test simple-maths
(is (= 2 (+ 1 2))))
并运行一个:
CL-USER> (in-package :proj1)
PROJ1> (run! 'SIMPLE-MATHS)
Running test suite ODDTEST
Running test SIMPLE-MATHS f
Did 1 check.
Pass: 0 ( 0%)
Skip: 0 ( 0%)
Fail: 1 (100%)
Failure Details:
[…]
运行测试套件:
PROJ1> (run! 'oddtest)
Running test suite ODDTEST
Running test suite ODDTEST
[…]
我们可以将所有内容放在
#+5am
功能标志下。