Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

BashScriptTestingLibrary:Unit testing framework for Shell scripts namely Bash

$
0
0
BashScriptTestingLibrary (BSTL)

...is a unit testing framework for Shell scripts - namely Bash.

This is a very light weight unit testing library for Bash scripts. It functions almost identically to Java's JUnit.

You do not need to touch any of your shell scripts to run unit tests against it. That is to say that your original source code does not need to know about the BSTL in order for the unit tests to work.

How to use:

Simply add the following lines underneath your shebang ( #!/bin/bash ) in your unit test file.

source /../BashScriptTestingLibrary.shl where it points to the location of the BSTL on your local machine.

source /../ScriptToTest.sh where ScriptToTest.sh is the original script you wanted to create unit tests for.

Next, you can create any test case you like, such as:

function testScriptVariableEquals5 () { assertEquals $ScriptVar 5 }

You can also evaluate that certian conditions have been met, such as a process was started:

function testMyProcessStarted () { assertTrue "pgrep myProcess.sh > /dev/null" }

At the very bottom of your unit test suite, you need to call the function runUnitTests .

Functions included in the BSTL

The BSTL includes all the assert functions found in Java's JUnit.

Specifically:

assertEquals expected actual

assertNotSame expected actual

assertNull actual

assertNotNull actual

assertTrue "condition expression"

assertFalse "condition expression"

Important Notes:

function "" "-f someFile.txt" BashScriptTestingLibraryUnitTests.ut


Viewing all articles
Browse latest Browse all 11063

Trending Articles