利用linux shell script来测试linux c程序------脚本自动化测试用例代替手动测试用例

来源:互联网 发布:定格动画制做软件 编辑:程序博客网 时间:2024/06/05 22:52

       我们来看一个简单的C程序:

#include <stdio.h>#include <string.h>int isGoodString(const char *p){if(strstr(p, "Good")){return 0;}return -1;}int main(int argc, char *argv[]){if(2 != argc){printf("para error\n");return -1;}if(0 == isGoodString(argv[1])){printf("yes\n");return 0;}printf("no\n");return 1;}
      现在, 我们来测试一下:

[taoge@localhost learn_c]$ gcc test.c [taoge@localhost learn_c]$ ./a.out para error[taoge@localhost learn_c]$ ./a.out 12no[taoge@localhost learn_c]$ ./a.out goodno[taoge@localhost learn_c]$ ./a.out Good123yes[taoge@localhost learn_c]$ ./a.out Goodyes[taoge@localhost learn_c]$ 

      我们可以看到, 这些测试用例是手动的。 手动测试用例的缺点是:

      1. 手动测试用例不便于保存(假设一个月后, 要再测一遍, 还得再敲一次。 当然, 你可能说, 你会保存这些文本, 但那样也需要复制命令到shell中重新运行)

      2. 手动测试用例很麻烦, 稍微不注意就会出错,没有一气呵成的感觉,  不利于自动化测试。


      对了, 前面不是一直在说脚本脚本么, 现在用脚本来搞一下自动化测试用例:

#!/bin/sh$1echo ""x=goodecho "$x"$1 "$x"x=goodbyeecho "$x"$1 "$x"x=Goodecho "$x"$1 "$x"x=Goodbyeecho "$x"$1 "$x"x="Good bye"echo "$x"$1 "$x"

    结果为:

[taoge@localhost learn_c]$ lsa.out  test.c  test.sh[taoge@localhost learn_c]$ ./test.sh ./a.out para errorgoodnogoodbyenoGoodyesGoodbyeyesGood byeyes[taoge@localhost learn_c]$ 

      自动化, 真是好啊, 一劳永逸。




0 0
原创粉丝点击