当前位置: 首页 > 知识库问答 >
问题:

从Clojure Repl和Leiningen运行测试

梁丘波鸿
2023-03-14

作为Clojure的新手,我使用leiningen创建了一个示例项目

lein new app first-project

它给了我这个目录

.
├── doc
│   └── intro.md
├── LICENSE
├── project.clj
├── README.md
├── resources
├── src
│   └── first_project
│       └── core.clj
├── target
│   └── repl
│       ├── classes
│       └── stale
│           └── extract-native.dependencies
└── test
    └── first_project
        └── core_test.clj

在不修改任何文件的情况下,我可以使用

lein test
...
Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Tests failed.

但是我无法使用运行测试从REPL做同样的事情

lein repl
first-project.core=> (use 'clojure.test)
nil
first-project.core=> (run-tests)

Testing first-project.core

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
{:type :summary, :pass 0, :test 0, :error 0, :fail 0}

我试过(但不起作用)

(require 'first-project.core-test)

共有3个答案

尉迟高澹
2023-03-14
匿名用户

重述:

要求的方式

只有在您先前在ns中发布了时,才需要对功能进行全面鉴定。然后做:

(clojure.core/require '[clojure.core :refer [require]]
                      '[clojure.test :refer [run-tests]]
                      '[clojure.repl :refer [dir]])
; Load whatever is in namespace "foo.bar-test" and reload everything
; if `:reload-all` has been additionally given

(require 'foo.bar-test :reload-all) 
;=> nil

; List your tests for good measure (Note: don't quote the namespace symbol!)

(dir foo.bar-test)
;=> t-math
;=> t-arith
;=> t-exponential
;=> nil 

; Check meta-information on a test to verify selector for example 

(meta #'foo.bar-test/t-math)
;=> {:basic-math true, :test #object[foo.bar_tes...

; `run-tests` will probably run nothing because the current namespace
; doesn't contain any tests, unless you have changed it with "in-ns"

(run-tests) 
;=> Ran 0 tests containing 0 assertions.

; run tests by giving namespace explicitly instead

(run-tests 'foo.bar-test) 
;=> Ran 3 tests containing 29 assertions.

敖涵容
2023-03-14

在上面的示例中,repl位于错误的命名空间中。如果您将repl切换到core_test命名空间,然后运行(run-test),可能会更好。

(in-ns 'first-project.core-test)
(run-tests)

开发测试的另一种有趣的方法是从REPL运行它们,直到它们工作为止,因为测试是带有一些额外元数据的正常函数。

(in-ns 'first-project.core-test)
(my-test)

请记住,除了在ns中调用之外,您还必须加载文件,假设您的测试文件是tests/first\u project/core\u test。clj,然后您需要呼叫

(load "tests/first_project/core_test")
(in-ns 'first-project.core-test)
(my-test)

请记住,文件系统中的\u在命名空间中变成-,而/变成

尉迟轶
2023-03-14

使用lein repl启动REPL,然后使用要求

(require '[clojure.test :refer [run-tests]])
(require 'your-ns.example-test :reload-all)
(run-tests 'your-ns.example-test)

我更喜欢呆在user名称空间中,而不是像另一个答案中提到的那样在ns中使用来更改名称空间。相反,将名称空间作为参数传递给运行测试(如上所示)。

我还建议远离(使用'clojure.test);这就是为什么我在上面建议(需要'[clojure.test:参考[运行测试]])。有关更多背景,请阅读http://dev.clojure.org/jira/browse/CLJ-879

 类似资料:
  • 我最近开始学习Clojure,想知道是否有一种执行简单. clj文件的标准方法。 我已经安装了Leiningen,并创建了我的第一个项目称为我的东西使用lein新应用程序我的东西。 管理我的东西。core,我从lein run开始: 接下来,我尝试了lein repl,接着是: 我还使用lein repl进行了一些基本评估: 我试着在我的东西里定义这个函数。核心: 我得到以下错误:clojure.

  • 问题内容: 我最近偶然发现了geb,这似乎是对我们的Web应用程序执行集成测试的好方法。我们的平台都是基于Java的,从阅读中可以得知 “ Geb通过与流行的测试框架(例如… JUnit,TestNG …)集成,为功能性Web测试提供了一流的支持。” 我认为从Java类执行测试很容易(testng测试?)。 我是groovy和geb的新手。 到目前为止,我在pom中包含了geb-testng和gr

  • 问题内容: 我希望能够从命令行运行Junit测试,但是当我运行此命令时 我回来的就是 它是否与Android项目有关?我之前已经运行过该命令,并且没有太多问题。 问题答案: 我只是设法从命令行运行JUnit测试,但是使用adb shell。 命令是 更多细节在这里。

  • 我有一些Spring测试,这些测试可以加速应用程序上下文并测试一些服务。我能够使用Maven并通过IDE运行这些测试。现在我需要在另一台无法访问Maven的机器上运行这些测试。我的想法是创建一个测试jar并通过命令行运行它们。 所以我创建了一个自定义Runner,它调用我需要的测试类,这些测试将启动Spring Application上下文并测试一些服务。 以下是示例代码: 我的自定义跑步者: 上

  • 问题内容: 是否可以使用leiningen在项目中与Clojure一起轻松管理和编译本机Java类? 我的工作水平很低(使用netty nio),并且认为某些管道类实际上在构造代码和性能方面都更容易作为原始Java处理。 问题答案: 在Leiningen教程中有以下声明 对于包含某些Java代码的项目,可以将project.clj中的:java-source- path键设置为包含Java文件的目

  • 我正在试验如何与maven surefire和testng并行运行测试。然而,配置似乎不是很简单,我无法让它工作。下面是我的虚拟测试。 这是我的surefire配置: 测试基本上是按顺序运行的。以下是日志作为证据: 我的意图是并行运行所有测试(直到方法级别)。那么,我该如何实现呢?