Bash and Test-Driven Development

前端 未结 8 1778
礼貌的吻别
礼貌的吻别 2020-12-04 15:33

When writing more than a trivial script in bash, I often wonder how to make the code testable.

It is typically hard to write tests for bash code, due to the fact tha

8条回答
  •  一向
    一向 (楼主)
    2020-12-04 16:02

    You might want to take a look at cucumber/aruba. Did quite a nice job for me.

    Additionally, you can stub just about everything you want by doing something like this:

    #
    # code.sh
    #
    some_function_calling_some_external_binary()
    {
        if ! external_binary action_1; then
            # ...
        fi  
    
        if ! external_binary action_2; then
            # ...
        fi
    }
    
    #
    # test.sh
    #
    
    # now for the test, simply stub your external binary:
    external_binary()
    {
        if [ "$@" = "action_1" ]; then
            # stub action_1
    
        elif [ "$@" = "action_2" ]; then
            # stub action_2
    
        else
            external_binary $@
        fi  
    }
    

提交回复
热议问题