加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell

发布时间:2020-12-15 09:12:09 所属栏目:安全 来源:网络整理
导读:利用linux shell script来测试linux c程序------脚本自动化测试用例代替手动测试用例 原创 2015年05月16日 23:21:17 我们来看一个简单的C程序: [cpp] view plain copy #includestdio.h #includestring.h int isGoodString( const char *p) { if (strstr(p,

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

原创 2015年05月16日 23:21:17

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

[cpp] view plain copy
  1. #include<stdio.h>
  2. #include<string.h>
  3. intisGoodString(constchar*p)
  4. {
  5. if(strstr(p,"Good"))
  6. {
  7. return0;
  8. }
  9. return-1;
  10. }
  11. intmain(intargc,char*argv[])
  12. {
  13. if(2!=argc)
  14. {
  15. printf("paraerrorn");
  16. return-1;
  17. }
  18. if(0==isGoodString(argv[1]))
  19. {
  20. printf("yesn");
  21. return0;
  22. }
  23. printf("non");
  24. return1;
  25. }
现在, 我们来测试一下:
[plain] view plain copy
  1. [taoge@localhostlearn_c]$gcctest.c
  2. [taoge@localhostlearn_c]$./a.out
  3. paraerror
  4. [taoge@localhostlearn_c]$./a.out12
  5. no
  6. [taoge@localhostlearn_c]$./a.outgood
  7. no
  8. [taoge@localhostlearn_c]$./a.outGood123
  9. yes
  10. [taoge@localhostlearn_c]$./a.outGood
  11. yes
  12. [taoge@localhostlearn_c]$

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

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

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


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

[plain] view plain copy
  1. #!/bin/sh
  2. $1
  3. echo""
  4. x=good
  5. echo"$x"
  6. $1"$x"
  7. x=goodbye
  8. echo"$x"
  9. $1"$x"
  10. x=Good
  11. echo"$x"
  12. $1"$x"
  13. x=Goodbye
  14. echo"$x"
  15. $1"$x"
  16. x="Goodbye"
  17. echo"$x"
  18. $1"$x"

结果为:
[plain] view plain copy
  1. [taoge@localhostlearn_c]$ls
  2. a.outtest.ctest.sh
  3. [taoge@localhostlearn_c]$./test.sh./a.out
  4. paraerror
  5. good
  6. no
  7. goodbye
  8. no
  9. Good
  10. yes
  11. Goodbye
  12. yes
  13. Goodbye
  14. yes
  15. [taoge@localhostlearn_c]$
自动化, 真是好啊, 一劳永逸。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读