Test Anything Protocol in Shell scripts

青春壹個敷衍的年華 提交于 2020-01-21 00:19:11

问题


Has anyone seen, tried to implement, or otherwise played with TAP in shell? We're looking to create unit tests across many languages (don't get me started on why this doesn't exist so far), and since we have so much Perl code, we'll be looking at TAP (among others, I imagine). I've found a TAP library for C, Perl, of course, has it built-in, and I've even found an API for Java. But one area missing is shell script testing.

Not that I've found much on unit-testing shell scripts, either, but since we do have thousands of lines of shell code, it'd be nice to be able to test it somehow.


回答1:


See the list of TAP Producers for a list of libraries. On that list you will find Tap-functions for shell code.




回答2:


I usually write my own small test framework for my shell scripts. Some things to keep in mind when doing this:

  • When working with files, make all paths relative to some variable which you can modify in your tests.
  • diff(1) is great to verify test results (and to display a useful error message to the user)
  • Use local variables extensively
  • Everything must be in a function

That said, my "test framework" is mostly a set of shell functions (named test*) and a runTests function which calls them one by one. Nothing fancy, really. Tests create a work directory for the test, copy all necessary files into it, run a function, verify the results against a know-good set of files.




回答3:


  • Bats is simple Bash only test framwork, tests could be written in a very clear syntax.
  • shUnit is the oldest shell test framework, little documentation.
  • shUnit2 is a most recently project inspired by shUnit, but completely different. Tests could be written in a more xUnit fashion. Most importantly, it is POSIX compatible.


来源:https://stackoverflow.com/questions/745266/test-anything-protocol-in-shell-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!