textbash的演示单元测试,只是个人使用(代码片段)

author author     2022-11-30     209

关键词:

#!/bin/bash

source ./testf.bashlib

test1()
    echo "hello world"


test_init_dir_unexist()
    [ -e ./test9 ] && rm -r ./test9
    init_dir
    rslt="$(ls -d ./test9)"
    assert_equal_str "./test9" "$rslt"


test_init_dir_exist_no_op()
    opt=""
    [ ! -e ./test9 ] && mkdir ./test9
    rslt=$(init_dir)
    n_words=$(get_first_n_words_by_space "$rslt" 3)
    assert_equal_str "Cannot create dir::" "$n_words"
    opt="-o"


test_init_dir_exist_op()
    [ ! -e ./test9 ] && mkdir ./test9
    opt="-o"
    rslt=$(init_dir)
    assert_equal_str "" "$rslt"


test_assert_equal_str()
    assert_equal_str "a" "a"
    assert_equal_str 1 1
    assert_equal_str "" ""
    assert_equal_str '' ""
    assert_equal_str ''
    
    rslt=$(assert_equal_str "a" "b")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"


test_assert_equal_num()   
    assert_equal_num 1 1
    assert_equal_num "1" "1"

    rslt=$(assert_equal_num "" "")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"

    rslt=$(assert_equal_num 1 "")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"
    
    rslt=$(assert_equal_num "" 1)
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"

    rslt=$(assert_equal_num 1 2)
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"
    
    rslt=$(assert_equal_num "a" "a")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"


set -- -o
opt="$1"

#run_tests test_assert_equal_num

tests1="test1 test_init_dir_unexist test_init_dir_exist_no_op test_init_dir_exist_op"
tests2="test_assert_equal_str test_assert_equal_num"
tests="$tests1 $tests2"
run_tests $(echo $tests)
#!/bin/bash

# ####test_demo_testf.bash
# #!/bin/bash
# 
# # Usage:
# # ./test_demo_testf.bash
# 
# # Use the -o to override the check on the test dir
# # ./test_demo_testf.bash -o
#
# source ./testf.bash
# 
# test1()
#     echo "hello"
# 
# 
# test2()
#     echo "world"
# 
# 
# opt="$1"
# run_tests "test1" "test2"


echo_start()
    echo "$1 ============================="


get_bwd()
    # bwd: base working dir
    # cwd: current working dir
    cwd=$(realpath .)
    : $bwd=$cwd


init_time_log()
    fpath_log="$bwd"/time.log
    if [ -f $fpath_log ]
        then
        if [ "-o" != "$opt" ]
            then
            echo "Cannot create file:: File $fpath_log exists. Exit."
            exit 1
        fi
        
        rm $fpath_log
    fi

    touch $fpath_log


log_datetime()
    datef=`date '+%Y%m%d.%H%M%S'`
    echo "$1$datef" >> $fpath_log


init_dir()
    dpath_test="$bwd"/test9
    if [ -e $dpath_test ]
        then
        if [ "-o" != "$opt" ]
            then
            echo "Cannot create dir:: Dir (or file) $dpath_test exists. Exit."
            exit 1
        fi
        
        rm -r $dpath_test
    fi
    
    mkdir $dpath_test
    cd $dpath_test


init_script_dir()
    # to be overwritten by the calling script
    init_dir


init_script_extra()
    # to be overwritten by the calling script
    :


finish_script_extra()
    # to be overwritten by the calling script
    :


init_script()
    echo_start "Start script $0"
    get_bwd
    init_time_log
    log_datetime "Start script $0 : "  
    init_script_dir  
    init_script_extra


finish_script()
    finish_script_extra
    echo_start "Finish script $0"
    log_datetime "Finish script $0 : "


init_test_extra()
    # to be overwritten by the calling script
    :


finish_test_extra()
    # to be overwritten by the calling script
    :


init_test()
    echo_start "Start $1 : "
    log_datetime "Start $1 : "
    init_test_extra


finish_test()
    finish_test_extra
    log_datetime "Finish $1 : "


run_tests()
    init_script

    for test in "$@"
        do
        init_test "$test"
        $test
        finish_test "$test"
    done

    finish_script


get_first_n_words_by_space()
    line="$1"
    n="$2"
    echo $(printf "$line" | cut -d " " -f 1-"$n")


assert_equal_str()
    expected="$1"
    actual="$2"
    if [ "$expected" == "$actual" ]
        then
        echo Pass
        return 0
    else  
        echo "Fail: $expected != $actual"
        return 1
    fi


assert_equal_num()
    expected="$1"
    actual="$2"

    if [[ "y" == "y$expected" && "y"=="y$actual" ]]
        then
            echo "Fail: arg1:expected is '', arg2:actual is ''"
        return 1
    fi
    
    if [ "y" == "y$expected" ]
        then
        echo "Fail: arg1:expected is ''"
        return 1
    fi
    
    if [ "y" == "y$actual" ]
        then
        echo "Fail: arg2:actual is ''"
        return 1
    fi
    
    if [ $expected -eq $actual ]
        then
        echo Pass
    else  
        echo "Fail: $expected != $actual"
        return 0
    fi


test1()
    # call the tested script
    :

androidcompose单元测试(代码片段)

上一个只是做了一个简单的演示,现在我扩展一下,做一个页面的单元测试,就是登录页面,校验页面单元测试内容登录输入框校验密码输入框校验网络请求校验@MediumTest@RunWith(JUnit4::class)classAppUITests@get:R... 查看详情

个人技术总结随笔(代码片段)

Junit(单元测试)Junit是java语言中用于单元测试的测试框架首先是使用junit测试框架,需要在项目里导入junit.jar然后使用注解的方式来标记方法,在方法前加上@Test表示这个方法是一个测试方法。我们在这里举一个简单的加法的例... 查看详情

个人技术和流程

   1.单元测试  软件是由多人合作完成的,不同人员的工作相互有依赖关系。单元测试是模块质量稳定和量化的保证。  好的单元测试的标准:      (1)单元测试应该在最基本的功能/参数上验证程序的... 查看详情

单元测试基本使用争议篇

介绍常用的单元测试是测试方法、API等,下面我们来演示一下Xunit测试框架的简单使用,有些是为了演示而写的单元测试。最下面有反转,一定要看到最后操作创建单元测试项目本次文章还在原来项目的基础上进行操... 查看详情

单元测试及框架简介--junitjmockmockitopowermock的简单使用(代码片段)

转单元测试及框架简介--junit、jmock、mockito、powermock的简单使用2013年08月28日14:33:06luvinahlc阅读数:6413标签:测试工具单元测试Junit实例Mockito更多个人分类:单元测试推荐一个新手学习Junit4的博客地址:http://www.cnblogs.com/eggbucket/arc... 查看详情

textbash脚本显示k8s的pvc使用情况(代码片段)

查看详情

androidcompose单元测试(代码片段)

上一个只是做了一个简单的演示,现在我扩展一下,做一个页面的单元测试,就是登录页面,校验页面单元测试内容登录输入框校验密码输入框校验网络请求校验@MediumTest@RunWith(JUnit4::class)classAppUITests@get:R... 查看详情

(30)c#使用nunit单元测试

单元测试是软件开发过程中一个关键的环节,很多软件开发人员基本上都会忽视这方面的开发,其实一个功能完成了,就直接丢给别人使用,或者自己使用,这样是不符合逻辑的。因为开发完成一个功能,这个功能到底有没有按... 查看详情

AFNetworking 单元测试

】AFNetworking单元测试【英文标题】:AFNetworkingUnitTests【发布时间】:2014-08-1517:40:47【问题描述】:我正在尝试使用以下代码进行单元测试。但是,测试只是挂在等待部分。我的测试代码如下。我是iOS和AFNetworking的新手。任何帮助... 查看详情

《构建之法》——个人技术和流程

#一、单元测试单元测试的作用:让自己负责的模块功能定义尽量明确,模块内部的改变不会影响其他模块,而且模块的质量能得到稳定的、量化的保证。##1.1好的单元测试的标准单元测试应该在最基本的功能/参数上验证程序的... 查看详情

你用啥来单元测试C代码? [复制]

】你用啥来单元测试C代码?[复制]【英文标题】:WhatdoyouusetounittestCcode?[duplicate]你用什么来单元测试C代码?[复制]【发布时间】:2010-10-0408:48:40【问题描述】:我是一名专业的网络开发人员,因此我习惯于使用非常高级的脚本语... 查看详情

go语言系统-从文件操作到单元测试(代码片段)

...入流和输出流打开文件和关闭文件使用的函数和方法案例演示读文件操作应用案例写文件操作应用案例判断文件是否存在文件编程应用实例拷贝文件统计英文、数字、空格和其它字符数量命令行参数举例说明flag包用来解析命令... 查看详情

从单元测试的角度来看:视图应该指定演示者还是 GWT MVP 中的其他方式?

】从单元测试的角度来看:视图应该指定演示者还是GWTMVP中的其他方式?【英文标题】:Inaunittestperspective:shouldtheviewspecifypresenterortheotherwayaroundinGWTMVP?【发布时间】:2011-08-1604:28:04【问题描述】:在有关GWT的教程中,Google使用不... 查看详情

junit单元测试(代码片段)

...@After:*修饰的方法会在测试方法执行之后自动被执行2代码演示2-1创建一个计算器类packagecn.itcast.junit;/***计算器类*/publicclassCalculator/***加法*@parama*@paramb*@return*/publicintadd(inta,intb)//inti=3/0;returna+b;/***减法*@parama*@paramb*@return*/publicintsub(i... 查看详情

聊聊单元测试(代码片段)

...结果返回。一个常见的代码笔者这里用Go写一个伪代码来演示,忽略那些特有的语法,相信单 查看详情

个人项目(代码片段)

...个简单而完整的软件工具(源程序特征统计程序)。进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。2.功能列表... 查看详情

springboot2---单元测试(junit5)(代码片段)

单元测试JUnit5的变化JUnit5常用注解常用注解使用演示断言机制(assertions)1、简单断言2、数组断言3、组合断言4、异常断言5、超时断言6、快速失败4、前置条件(assumptions)5、嵌套测试6、参数化测试Junit4到Junit5的迁移指南JUn... 查看详情

做个人吧,写点testable代码,好吗!

标题所说的Testable实际上指的是UnitTestable,也就是可单元测试,或者是易书写单元测试的代码。注:不知道单元测试(UnitTest)是什么的,请自行Google需求解析那怎样才算是UnTestable的代码呢?又是什么原因导致很... 查看详情