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

972 篇文章 329 订阅
461 篇文章 67 订阅

       我们来看一个简单的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 12
no
[taoge@localhost learn_c]$ ./a.out good
no
[taoge@localhost learn_c]$ ./a.out Good123
yes
[taoge@localhost learn_c]$ ./a.out Good
yes
[taoge@localhost learn_c]$ 


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

 

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

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

 

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

 

#!/bin/sh

$1
echo ""

x=good
echo "$x"
$1 "$x"

x=goodbye
echo "$x"
$1 "$x"

x=Good
echo "$x"
$1 "$x"

x=Goodbye
echo "$x"
$1 "$x"

x="Good bye"
echo "$x"
$1 "$x"


    结果为:

 

 

[taoge@localhost learn_c]$ ls
a.out  test.c  test.sh
[taoge@localhost learn_c]$ ./test.sh ./a.out 
para error

good
no
goodbye
no
Good
yes
Goodbye
yes
Good bye
yes
[taoge@localhost learn_c]$ 


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

 

 

 

 

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值