开发者

Testing a bash shell script [duplicate]

开发者 https://www.devze.com 2023-02-18 23:39 出处:网络
This question already has answers here: Bash and Test-Driven Development (8 answers) Unit testing Bash scripts
This question already has answers here: Bash and Test-Driven Development (8 answers) Unit testing Bash scripts (16 answers) Closed 5 years ago.

Can someone explain how to test for a bash shell script?

For example i've got a .sh file with this code in it...

#!/bin/sh

for file in *.txt; do
    mv "$file" "`basename $file .txt`.doc"
done

How do I write a test for it? Like in Java you'开发者_如何学Pythonve got unit testing where you write code like assertEquals to test the code gives the desired output.


Try this out: assert.sh

source "./assert.sh"

local expected actual
expected="Hello"
actual="World!"
assert_eq "$expected" "$actual" "not equivalent!"
# => x Hello == World :: not equivalent! 


You can do asserts in Bash. Check out this from the Advanced Bash-Scripting Guide:

http://tldp.org/LDP/abs/html/debugging.html#ASSERT


I'd add an echo in front of the mv to verify that the right commands are being created, for starters. (Always a good idea with commands that make possibly difficult to undo changes.)

Some possibly useful resources:

  • shUnit2 — xUnit framework for script unit testing
  • Bash IDE for Vim
  • Bash debugger
0

精彩评论

暂无评论...
验证码 换一张
取 消