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

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

问题描述

这些年来,我从事的几乎所有产品都涉及到一定程度的Shell脚本(或Windows上的批处理文件,PowerShell等).即使我们用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.

推荐答案

更新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天全站免登陆