shell 脚本的单元测试 [英] Unit testing for shell scripts

查看:42
本文介绍了shell 脚本的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些年来我开发的几乎所有产品都涉及某种级别的 shell 脚本(或批处理文件、PowerShell 等,在 Windows 上).尽管我们用 Java 或 C++ 编写了大量代码,但似乎总有一些集成或安装任务最好使用 shell 脚本来完成.

Pretty much every product I've worked on over the years has involved some level of shell scripts (or batch files, PowerShell etc. on Windows). Even though we wrote the bulk of the code in Java or C++, there always seemed to be some integration or install tasks that were better done with a shell script.

因此,shell 脚本成为交付代码的一部分,因此需要像编译后的代码一样进行测试.有没有人使用过一些现有的 shell 脚本单元测试框架,例如 shunit2 ?我现在主要对 Linux shell 脚本感兴趣;我想知道测试工具如何复制其他 xUnit 框架的功能和易用性,以及与 CruiseControl 或 Hudson 等持续构建系统集成的难易程度.

The shell scripts thus become part of the shipped code and therefore need to be tested just like the compiled code. Does anyone have experience with some of the shell script unit test frameworks that are out there, such as shunit2 ? I'm mainly interested in Linux shell scripts for now; I'd like to know how well the test harness duplicate the functionality and ease of use of other xUnit frameworks, and how easy it is to integrate with continuous build systems such as CruiseControl or Hudson.

推荐答案

UPDATE 2019-03-01:我的偏好是 蝙蝠 现在.我已经在小项目上使用它几年了.我喜欢干净、简洁的语法.我没有将它与 CI/CD 框架集成,但它的退出状态确实反映了套件的整体成功/失败,如下所述比 shunit2 更好.

UPDATE 2019-03-01: My preference is bats now. I have used it for a few years on small projects. I like the clean, concise syntax. I have not integrated it with CI/CD frameworks, but its exit status does reflect the overall success/failure of the suite, which is better than shunit2 as described below.

以前的答案:

我将 shunit2 用于与 Linux 环境中的 Java/Ruby Web 应用程序相关的 shell 脚本.它易于使用,与其他 xUnit 框架没有太大区别.

I'm using shunit2 for shell scripts related to a Java/Ruby web application in a Linux environment. It's been easy to use, and not a big departure from other xUnit frameworks.

我没有尝试与 CruiseControl 或 Hudson/Jenkins 集成,但在通过其他方式实现持续集成时,我遇到了以下问题:

I have not tried integrating with CruiseControl or Hudson/Jenkins, but in implementing continuous integration via other means I've encountered these issues:

  • 退出状态:当测试套件失败时,shunit2 不使用非零退出状态来传达失败.因此,您要么必须解析 shunit2 输出以确定套件的通过/失败,要么将 shunit2 更改为某些持续集成框架所期望的行为,通过退出状态传达通过/失败.
  • XML 日志:shunit2 不会生成 JUnit 样式的 XML 结果日志.

这篇关于shell 脚本的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆