运行 测试子模块作为主钩子

Running test submodule as main hook

鉴于此代码:

#lang racket/base

(module+ test
  (require rackunit rackunit/text-ui)

  (provide suite)

  (define suite
    (test-suite
     "test tests"

     (test-equal? "test string test"
                  "string"
                  "string")))

  (run-tests suite))

;(require 'test)
;(suite)

如果最后两行留下注释并且文件是 运行 和 raco test test.rkt,将输出

raco test: (submod "test.rkt" test)
1 success(es) 0 failure(s) 0 error(s) 1 test(s) run
0
1 test passed

这是预期的。

当文件 运行 只是作为脚本而不是 raco 时,我如何使文件 运行 成为测试文件?

我以为末尾的两行注释会做我想做的事:导入子模块然后调用函数,

(require 'test)
(suite)

但我得到:

$ racket test.rkt
require: unknown module
  module name: #<resolved-module-path:'test>
  context...:
   standard-module-name-resolver

Learn Racket in Y Minutes 似乎说 'test 作为 'symbol 用于子模块,但也许不是。

module+module* 声明的子模块在它们的包含模块中对 require 不可用,因为它们可以依赖于它们的包含模块,并且模块依赖图中的循环是不允许。 (相比之下,用 module 声明的子模块不能依赖于它们的包含模块,但它们的包含模块可以 require 它们。)

尝试添加一个 main 子模块;当文件是 运行 作为脚本时应该得到 运行:

(module+ main
  (require (submod ".." test))
  (run-tests suite))

顺便说一句,Racket 约定是 test 子模块到 运行 测试,而不仅仅是定义它们。添加 main 子模块可能会使 raco test 停止为您的脚本工作;解决方法是将 (run-tests suite) 调用移至 test 子模块。