单元测试 Bash 脚本 [英] Unit testing Bash scripts

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

问题描述

我们有一个系统,除了 Java 代码之外,还有一些 Bash 脚本在运行.由于我们正在尝试测试所有可能损坏的东西,而那些 Bash 脚本可能会损坏,因此我们想测试它们.

We have a system that has some Bash scripts running besides Java code. Since we are trying to test everything that could possibly break, and those Bash scripts may break, we want to test them.

问题是很难测试 Bash 脚本.

The problem is it is hard to test Bash scripts.

是否有测试 Bash 脚本的方法或最佳实践?还是我们应该停止使用 Bash 脚本并寻找可测试的替代解决方案?

Is there a way or a best practice to test Bash scripts? Or should we quit using Bash scripts and look for alternative solutions that are testable?

推荐答案

我从一个讨论组得到以下答案:

I got the following answer from a discussion group:

可以导入(包括,无论如何)一个过程(函数,无论它叫什么名字)来自外部文件.这是写一个的关键测试脚本:你打破你的脚本成独立程序然后可以导入到两者中你的运行脚本和你的测试脚本,然后运行脚本尽可能简单.

it's possible to import (include, whatever) a procedure (function, whatever it's named) from an external file. That's the key to writing a testing script: you break up your script into independent procedures that can then be imported into both your running script and your testing script, and then you have your running script be as simple as possible.

这种方法类似于脚本的依赖注入,听起来很合理.最好避免使用 Bash 脚本并使用更可测试且更少晦涩的语言.

This method is like dependency injection for scripts and sounds reasonable. Avoiding Bash scripts and using more testable and less obscure language is preferable.

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

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