Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write unit tests for Bash scripts

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to write unit tests for Bash scripts, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Why write unit tests for Bash scripts?

Because Bash scripts usually perform some operations related to the operating system, it may cause some irreversible operations to the running environment, such as modifying or deleting files, upgrading software packages in the system, and so on.

So in order to ensure the security and reliability of the Bash script, you must do enough testing before deploying it in a production environment to ensure that its behavior meets our expectations.

How can you test Bash scripts safely and reliably? Some people might say that we can use Docker containers. Yes, it is safe and convenient to do so. In a container-isolated environment, you don't have to worry about scripts damaging our system, and you can easily and quickly rebuild an available test environment. However, consider the following common scenarios:

Scenario 1: before executing the Bash script test, we need to install all the third-party tools that will be used in the Bash script, otherwise the test will fail because the command cannot be found. For example, we used Bazel as a build tool in the script. We must install and configure Bazel in advance, and don't forget that in order to work properly with Bazel, we need a project that supports building using Bazel.

Scenario 2: the stability of the test results may depend on the stability of the third-party services accessed in the script. For example, we use the curl command in a script to get data from a network service, but that service may sometimes fail to access. It may be caused by the instability of the network, or it may be because the service itself is unstable. Or if we need a third-party service to return different data to test the different branching logic of the script, but it may be difficult for us to modify the data of this third-party service.

Scenario 3: the execution time of the test case of the Bash script depends on the execution time of the commands used in the script. For example, if we use Gradle in our script to build a project, a build of Gradle may take 3 minutes or 3 hours depending on the size of the project. This is just a test case, what if we have 20 or 100 test cases? Can we still get the test report in a few seconds?

Even if a container is used to perform Bash scripting tests, some of the above problems cannot be avoided. The preparation process of the environment may become cumbersome with the increase of test cases. the stability and execution time of test cases depend on the stability and execution time of third-party commands and services. it may also be difficult to use different data to cover different test scenarios.

For testing Bash scripts, what we really want to verify is the execution logic of Bash scripts. For example, in the Bash script, the options and parameters of internally invoked commands may be combined based on the passed parameters, and what we want to verify is that these options and parameters are exactly what we expected. As for why the invoked command fails after accepting these options and parameters, we may not care about all of these possible reasons.

Because there will be more external factors, such as whether the hardware and network are working properly, whether the third-party services are working properly, whether the compilers needed to build the project are installed and configured properly, whether the authorization and authentication information are valid, and so on. But for Bash scripts, the result of these external reasons is that the invoked command succeeds or fails. So all the Bash script needs to care about is whether the commands called in the script can be executed successfully, what the commands output, and which different branch logic in the script is then executed. Phellodendron mandshurica (Thunb.)

What if we just want to know if this command with these option parameters will work as we expected? It's simple, then execute it alone on the command line. If it doesn't work as expected on the command line, putting it in a Bash script won't work as expected. This error has little to do with the Bash script. Therefore, in order to minimize the external factors that affect Bash script validation, we should consider writing unit tests for Bash scripts to focus on the execution logic of Bash scripts.

What kind of test is the unit test of a Bash script?

First, all commands that exist in the path of the PATH environment variable should not be executed in the unit test. For Bash scripts, the commands that are called run normally, with return values and output. However, these commands called in the script are simulated and used to simulate the behavior of the corresponding real commands. In this way, we avoid a large part of the external dependency in the unit test of the Bash script, and the execution speed of the test is not affected by the real command. Second, each unit test case should be independent. This means that these test cases can be executed independently or arbitrarily out of order without affecting the verification results. Finally, these test cases can be executed on different operating systems and should all get the same verification results. For example, the Bash script uses commands that are only available on GNU/Linux, and the corresponding unit tests can also be executed on Windows or macOS, and the results are consistent.

How do I write unit tests for Bash scripts?

Like other programming languages, Bash has multiple testing frameworks, such as Bats, Shunit2, and so on, but these frameworks do not actually isolate commands from all PATH environment variables. A test framework called Bach Testing Framework is currently the only one that can write real unit tests for Bash scripts. The most unique feature of Bach Testing Framework is that no commands located in the PATH environment variable are executed by default, so Bach Testing Framework is ideal for validating the execution logic of Bash scripts. It also brings the following benefits:

simple

Nothing needs to be installed. We can run these tests. For example, you can execute a Bash script that invokes a large number of third-party commands in a completely new environment.

Come on!

Because none of the commands are actually executed, each test case is executed very quickly.

Safety

Because no external commands are executed, even if some error in the Bash script results in the execution of a dangerous command, such as rm-rf *. Bach will ensure that these dangerous commands will not be carried out.

Independent of the operating environment

You can perform tests on Windows for scripts that only work on GNU/Linux.

Due to some limitations of the operating system and Bash, Bach Testing Framework cannot do this:

Intercept commands called with absolute paths

In fact, we should avoid using absolute paths in Bash scripts. If it is inevitable, we can extract the absolute path as a variable, or put it into a function and simulate the function with @ mock API.

Intercept such as >, > >,

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report