首页 > 解决方案 > 使用 dune 运行 OUnit 测试

问题描述

我在运行 OUnit 测试时遇到了困难,主要是因为我对沙丘和 OUnit 都不熟悉。dune我跑步时抱怨dune runtest

File "test/dune", line 4, characters 13-14:
Error: Library "f" not found.
Hint: try: dune external-lib-deps --missing @runtest

这是项目结构:

├── dune
├── f.ml  # This is the source file.
└── test
    ├── dune
    └── f_test.ml  # This is the test.

这是dune

(executable
  (name f))

这是test/dune

(test
  (name f_test)
  (libraries oUnit f))  ; <- `f` here causes problems.

我可以看到出现错误是因为 dune 不知道f.ml,因此不知道fdune 文件中的 about 。

如何以了解我使用的库f.ml的方式进行 dune 编译?如何正确运行单元测试?test/duneftest/f_test.ml

标签: ocamlounitocaml-dune

解决方案


一种可能是拆分f成私有库和可执行文件,然后测试拆分库。

编辑:

例如,项目结构可以更新为

├── dune
├── f.ml  # f only contains the I/O glue code.
├── lib
|    ├── dune
|    └── a.ml  # a implements the features that need to be tested.
└── test
    ├── dune
    └── test.ml  # This is the test.

dune

 (executable (name main) (libraries Lib)) 

对于测试,test/dune

(test (name test) (libraries Lib oUnit))

最后lib/dune

(library (name Lib))

使用此设置,可以使用dune runtest.


推荐阅读