Production Level Knowledge & Tips

  1. 未分類
  2. 71 view

[Test][ShellScript]Write test code and measure C0 coverage for shell(bash) using shunit2 and shcov

Outline

Write test code and measure coverage for TDD with shell script.
I’ll use shunit2 for test and shcov for coverage.

Test

Install

wget https://shunit2.googlecode.com/files/shunit2-2.1.6.tgz
tar zxvf shunit2-2.1.6.tgz

Write test

vim firstTest.sh
---
#!/bin/bash

### firstTest.sh ###

function testWeCanWriteTests () {
    assertEquals "it works" "it works"
}

## Call and Run all Tests
. "./shunit2-2.1.6/src/shunit2"
---
chmod +x firstTest.sh
./firstTest.sh
---
testWeCanWriteTests

Ran 1 test.

OK

Coverage

Install

wget https://shcov.googlecode.com/files/shcov-5.tar.gz
tar zxvf shcov-5.tar.gz

Measure coverage

first.sh

#!/bin/bash

function Hello () {
  echo "Hello World!"
}

firstTest.sh

#!/bin/bash

## Load target shell
. first.sh

### firstTest.sh ###
function testWeCanWriteTests () {
    Hello
    assertEquals $? 0
}

## Call and Run all Tests
. "./shunit2-2.1.6/src/shunit2"

Measure

shcov-5/scripts/shcov firstTest.sh
---
testWeCanWriteTests
Hello World!

Ran 1 test.

OK
---

ls /tmp/shcov
shcov-5/scripts/shlcov /tmp/shcov output
ls output

Output

shcov

未分類 recent post

  1. Install sbt 1.0.0 and run sample template

  2. Visualization of Neural Network and its Train…

  3. [Machine Learning]Created docker image includ…

  4. [Node.js]How to write batch script with Node.…

  5. [Hive]Get top n items for each category

関連記事

PAGE TOP