Here comes a unit testing framework for Bourne shell code similar to junit framework for java. Lets digest it.
1) Download shunit framework from here http://sourceforge.net/projects/shunit/files/shunit/1.5/ShUnit-1.5.zip/download
2) Untar this downloaded framework in any directory. I l use "/home" directory
3) Set a variable SHUNIT_HOME that points to the directory where ShUnit's files are. E.g., add 'export SHUNIT_HOME=/home/ShUnit'
4) Lets create a simple script "print_something" which will print something. You can create this script in /home folder too.
#!/bin/sh
# print_something - a testable script
print_something()
{
echo "$1"
}
invalid_print_something()
{
echo "nothinggggg"
}
Here, method print_something will print the parameter passed to it. Method invalid_print_something will print "nothinggggg" always.
5) To unit test this script we need to test the functionality of the above two functions. Lets create a test script
#!/bin/sh
# print_something.test - unit tests for print_something
. "$SHUNIT_HOME/shUnit"
# Include script to be tested
. ./print_something.sh
TestScriptExists()
{
shuNonZeroFile <path of file to test>
}
TestPrintSomething()
{
EXPECTED="this is a test"
RESULT=`print_something "$EXPECTED"`
test "${EXPECTED}" = "${RESULT}"
shuAssert "Test print_something" $?
}
TestInvalidPrintSomething()
{
EXPECTED="this is a test"
RESULT=`invalid_print_something "$EXPECTED"`
test "${EXPECTED}" = "${RESULT}"
shuAssert "Test invalid print_something" $?
}
InitFunction()
{
shuRegest TestScriptExists
shuRegTest TestPrintSomething
shuRegTest TestInvalidPrintSomething
}
shuStart InitFunction
Lets see this test script step by step
# print_something.test - unit tests for print_something
. "$SHUNIT_HOME/shUnit"
We include shunit from frmaework
# Include script to be tested
. ./print_something.sh
Include script to be tested
TestPrintSomething()
{
EXPECTED="this is a first test"
RESULT=`print_something "$EXPECTED"`
test "${EXPECTED}" = "${RESULT}"
shuAssert "Test print_something" $?
}
This method intern calls print_something method of print_something script and tallys the expected result.
Other method works in similar way
InitFunction()
{
shuRegest TestScriptExists
shuRegTest TestPrintSomething
shuRegTest TestInvalidPrintSomething
}
Call out test methods
Call "TestScriptExists" method first to check that script exists.
shuStart InitFunction
Call InitFunction as starting function
6) Expecting files print_something.sh, print_something_test.sh in same directory, We now run our test script
[root@sidd siddhesh]# sh print_something_test.sh
****** print_something_test.sh ******
2 tests to run:
Test 1: TestPrintSomething .
Test 2: TestInvalidPrintSomething E
"Test invalid print_something" failed.
2 tests run.
1 test succeeded.
1 test failed.
7) Lets see some more properties of SHUNIT's
We saw functionality of "shuAssert "Test invalid print_something" $?"
it will pass and display message if $? turns out to be ZERO. What if we want to test few negative things. In this case will use
shuDeny "I am negative test case" $?
Here, Test will pass if value of $? comes out as 1
8) Following method is executed at the begining of the test script file
shuSetUp()
{
// Create your directories,change file permissions etc etc
}
9) Following method is executed at the end of the test script file
shuTearDown()
{
// Delete your directories,change file permissions etc etc
}
10) We have used shuassert like this
shuassert <statement> $?
This will pass if $? is 0
We can use 'shuDeny ' which passes if $? is 1.
11) I dont think mocking anything will be difficult task with shunits. I havent tried it out yet
..Lets see..
but as of now CHEERS.. Happy testing !!
分享到:
相关推荐
Tutorial 1: Setting up DirectX 11 with Visual Studio Tutorial 2: Creating a Framework and Window Tutorial 3: Initializing DirectX 11 Tutorial 4: Buffers, Shaders, and HLSL Tutorial 5: ...
《Computational Fourier Optics:a MATLAB tutorial》是关于傅里叶光学的一个教程,结合了MATLAB编程,旨在帮助读者深入理解和应用傅里叶光学的基本原理。傅里叶光学是光学领域的一个重要分支,主要研究光学系统...
integrate it into one of the popular integrated development environments (IDEs) called eclipse to make your work easier, thus increasing your productivity and project quality. This tutorial provides ...
ActiveX基础教学文件
深度学习教程 一。入门资料 :star: 数学基础 机器学习基础 快速入门 && && && && 深刻理解 && && &&&& ...经典机器学习算法:LR,KNN,SVM,随机森林,GBDT(XGBoost && LightGBM),分解机,现场感知分解机,神经...
13.03.2015 Eemil Lagerspetz, Ella Peltonen Professor Sasu Tarkoma These slides: http://is.gd/bigdatascala
C# Design Patterns: A Tutorial is a practical guide to writing C# programs using the most common patterns. This tutorial begins with clear and concise introductions to C#, object-oriented ...
Tutorial 一. 入门资料 :star: 数学基础 机器学习基础 快速入门 推荐顺序由前到后 && && && && 深入理解 推荐顺序由前到后 深度学习基础 快速入门 推荐顺序由前到后 && && 计算机视觉 && 自然...
Tutorial 一. 入门资料 :star: 数学基础 机器学习基础 快速入门 推荐顺序由前到后 && && && && 深入理解 推荐顺序由前到后 深度学习基础 快速入门 推荐顺序由前到后 && && 计算机视觉 && 自然...
Tutorial 一. 入门资料 :star: 数学基础 机器学习基础 快速入门 推荐顺序由前到后 && && && && 深入理解 推荐顺序由前到后 深度学习基础 快速入门 推荐顺序由前到后 && && 计算机视觉 && 自然...
Tutorial 一. 入门资料 :star: 数学基础 机器学习基础 快速入门 推荐顺序由前到后 && && && && 深入理解 推荐顺序由前到后 深度学习基础 快速入门 推荐顺序由前到后 && && 计算机视觉 && 自然...
manage_rr在Windows上安装mongoDB https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/项目设置npm install启动数据库mongorestore --db=manage_rr --archive="manage_rr"编译和热重装以进行开发...
Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: ...
forecasting_sample.zip Argoverse API Forecasting 包含少量的csv数据,用于调试
4. **计算傅立叶光学:MATLAB教程**(Computational Fourier Optics: A MATLAB® Tutorial): - **知识点**:本书是本次讨论的重点。它详细介绍了如何使用MATLAB进行傅立叶光学的模拟和分析,涵盖了从基本概念到...
在这个"C++ Tutorial: Sockets - Server & Client using QT - 2015"中,你将学习到如何利用QT的网络模块来实现基于TCP的通信。 首先,我们来看看QT中的`QTcpServer`类,它是用于创建服务器端应用的核心组件。`...
This book began as a collection of notes and computer examples prepared for a first-year graduate course on Fourier optics. In teaching Fourier optics over a number of years, I found that I developed ...
Title: Java 7: A Comprehensive Tutorial Author: Budi Kurniawan Length: 850 pages Edition: 1 Language: English Publisher: BrainySoftware Publication Date: 2014-05-15 ISBN-10: 0980839661 ISBN-13: ...