如何使自动工具测试读取文件?

uml*_*ute 5 automake unit-testing autotools

my autotools project has a couple of unit-tests. one of these tests (filereader) needs to read a file (data/test1.bin)

Here's my filesystem layout: - libfoo/tests/filereader.c - libfoo/tests/data/test1.bin

and my libfoo/tests/Makefile.am:

AUTOMAKE_OPTIONS = foreign
AM_CPPFLAGS = -I$(top_srcdir)/foo
LDADD = $(top_builddir)/src/libfoo.la

EXTRA_DIST = data/file1.bin

TESTS = filereader
check_PROGRAMS= filereader
filereader_SOURCES = filereader.c
Run Code Online (Sandbox Code Playgroud)

this works great, as long as i do in-tree builds. However, when running the test-suite out-of-tree (e.g. make distcheck), the filereader test cannot find the input file anymore.

This is obviously because only the source tree contains the input file, but not the build tree.

i wonder what is the canonical way to fix this problem?

  • compile the directory of the test-file into the unittest (AM_CPPFLAGS+=-DSRCDIR=$(srcdir))
  • pass the qualified input file as a cmdline argument to the test? (e.g. $(builddir)/filereader $(srcdir)/data/file1.bin)
  • 将输入文件从源树复制到构建树?(cp $(srcdir)/data/file1.bin $(builddir)/data/file1.bin?一个合适的制作规则会是什么样的??)

Die*_*enò 2

按照规范,解决方案是将文件的路径定义到单元测试中,因此您提出的第一个选项。第二种也是可能的,但它需要使用中间驱动程序脚本。

我建议避免第三种,但如果您确实想走那条路,请使用$(LN_S)而不是cp; 这样可以减少测试的 I/O 负载。