...is a unit testing framework for Shell scripts - namely Bash.
This is a very light weight (as in one file) unit testing library for Bash scripts. It functions similarly 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 bunit in order for the unit tests to work.
Simply add the following lines underneath your shebang (#!/bin/bash
) in your unit test file.
source /../bunit.shl
where it points to the location of the bunit script 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:
testScriptVariableEquals5 () {
someScriptFunctionThatSetsScriptVarToFive
assertEquals "$ScriptVar" 5
}
You can also evaluate that certian conditions have been met, such as a process was started:
testMyProcessStarted () {
someScriptFunctionThatStartsMyProcess
assertTrue "pgrep myProcess.sh > /dev/null"
}
At the very bottom of your unit test suite, you need to call the function runUnitTests
.
When you are ready to run your unit test suite, simply run it as you would any other script.
./MyUnitTestSuite.ut
Or you can run it verbosely using the -v
or --verbose
flags, which will print out all test case results, even if they pass.
./MyUnitTestSuite.ut -v
./MyUnitTestSuite.ut --verbose
Your unit test suite is a shell script! Therefore, if you run a dangerous command, you are going to get dangerous results! For example,
testCanDeleteHomeDirectory() {
rm -rf ~/
assertTrue " ! -d ~ "
}
This will absolutely delete your home directory and the assertTrue function will return a valid result, because you just blew away your home directory. Pay attention to your commands!
bunit includes most the assert functions found in Java's JUnit.
Specifically:
assertEquals expected actual
assertNotEquals expected actual
assertNull "actual"
assertNotNull "actual"
assertTrue "condition expression"
assertFalse "condition expression"
assertContains needle haystack
Only functions prefixed with test
, i.e: test<functionName>
inside of the unit test suite will be executed by runUnitTests
.
Null is considered to be empty string ""
. The assertNull requires you to enclose your argument in quotes.
You must include quotes around conditional expressions. These expressions can be anything that can be evaluated in an if statement condition. Example: "-f someFile.txt"
The extension .shl
is an extension I perfer to give to library shell scripts. It stands for .shellLibrary
.
The extension .ut
is an extension I prefer to give to unit test suites. It stands for .unitTest
.
Both of the above extensions are meaningless from a linux perspective, so you can change them to be whatever you like.
If all of your unit tests pass, and you did not run the script with the verbose flag, you will see no output at all.
If any of your unit tests fail, the script will exit after all tests have run, with a status code of 1.
If you need to test commands that delete files or directories, it is strongly recommended to use a testSetup()
function at the top of your unit test suite, and use it to create temporaryfiles or directories that you are intentionally trying to delete. This probably goes without saying but, DO NOT TEST rm
COMMANDS AGAINST PRODUCTION DATA!!!
If you wish, you can also use a testTearDown()
function at the bottom of your unit test suite, to clean up your tests.
You can change the name of testSetup()
and testTearDown()
. They are not hard coded. Refer to point #1.
Please see the unit tests in bunit_unit_tests
and in examples
for more examples.
bUnit介绍 bUnit是Blazor组件测试库。其目标是使编写全面、稳定的单位测试变得容易。有了bUnit,你可以: 使用C#或Razor语法设置和定义测试下的组件 使用语义HTML比较器验证结果 与组件以及触发事件处理程序进行交互和检查 传递参数、级联值并将服务注入测试组件 模拟IJSRuntime,认证和授权等 bUnit是建立在现有单元测试框架(如xUnit、NUnit和MSTest)基
1.dbunit简介 使用DbUnit,开发人员可以控制测试数据库的状态。进行一个DAO单元测试之前,DbUnit为数据库准备好初始化数据;而在测试结束时,DbUnit会把数据库状态恢复到测试前的状态 2.下载dbunit包 <dependency> <groupId>dbunit</groupId> <artifactId>dbunit</artifactId> <versio