如何从笔记中自动生成测试名称?

问题描述 投票:0回答:3

[在编写测试用例之前,我经常在将成为测试用例的文件中以注释的形式编写测试的描述。稍后,我将描述用作测试的名称。例如,

// a user can log in
// password is stored in the database

成为

function test_a_user_can_log_in() {
    // test code here...
}

function test_password_is_stored_in_the_database() {
    // test code here...
}

我通常重写句子并手动插入下划线,但是当然必须有更好的方法。

是否存在某种可以使之自动化的正则表达式,sed命令,shell管道等?

regex bash sed editing
3个回答
2
投票

使用sed,您可以使用类似

sed '/\/\//{ s/ /_/g; s|//|test|; s/$/ () \{\
  # test code here\
\}/; }' tmp.sh

0
投票

[这里是我的想法

function generate_test_cases() {
        while read l; do
                echo $l
                # check if line is a comment
                if [[ $l == //* ]]
                then
                        echo $l
                        # remove //
                        l=${l:2}
                        # replace spaces with underscores
                        l=${l// /_}
                        # append to filename.test.bash
                        echo "function $l() { \n }" >> $1.test.bash
                        echo $l
                fi
        done <$1
}

0
投票

另一个选项是awk,您将测试第一个字段是"//",如果是,则将记录写成带有注释正文的函数名称,例如]

awk '$1~/\/\// {
    $1 = "test"
    gsub(/ /,"_")
    print "function "$0"() {\n    // test code here...\n}\n" 
}' file

示例使用/输出

将示例输入file,您将收到:

awk '$1~/\/\// {
>     $1 = "test"
>     gsub(/ /,"_")
>     print "function "$0"() {\n    // test code here...\n}\n"
> }' file
function test_a_user_can_log_in() {
    // test code here...
}

function test_password_is_stored_in_the_database() {
    // test code here...
}
© www.soinside.com 2019 - 2024. All rights reserved.