Snake's Home

一次精准测试实践

本文发表在公众号: 软件测试精品

敏捷模式下迭代频繁,回归测试时总是不知道变动的范围。Dev 有的时候也不知道他改了哪些东西,影响到哪些节点,或者是很多人改的,彼此不知道。遇到有代码洁癖的,改了别人的代码,大家都不知道。通常情况是,要么测试范围定小了,遗漏了;要么测试范围过大,付出过多代价。每次回归,Tester 心里总没底,生怕漏了哪里。如何才能准确定位到变更范围呢?

作者尝试通过对source code 的变动,来达到了解应用的功能模块变化范围。从而制定回归范围和smoke范围。现在大多数公司用git, 本文就以git 为例。在git 中,用这条命令,就可以查看source change log.

1
git whatchanged

例如:

1
git whatchanged --since='2 weeks ago'

就可以得到最近2周所有commit 的信息。
首先对comments 进行统计。进行词频分析。

1
2
3
4
5
6
7
8
9
def get_words_dict(target):      
table = {}
line, number = re.subn(r"\W", " ", target)
for word in line.split():
if table.has_key(word):
table[word] += 1
else:
table[word] = 1
return table

去除无效的词,自己定义一个list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
remove_words = ['the', 'a', 'bug', 'to', 'of', 'so', 'one', 'more', 'we', 'Update', 'app', 'our', 'issue','want', 'work']
def remove_dict(obj, key):
if key in obj.keys():
obj.pop(key)for word in remove_words:
remove_dict(words_number, word)
这个词库可以根据需要增加。然后生成一张图表:

def get_words_graphic(wordlist):
keylist = wordlist.keys()
vallist = wordlist.values()
barwidth = 0.3
xVal = numpy.arange(len(keylist))
plt.xticks(xVal + barwidth / 2.0, keylist, rotation=90)
plt.bar(xVal, vallist, width=barwidth, color='y')
plt.title(u'词频分析图')
plt.show()

为了防止图片展示不清楚,同时还写一份log.
这样,就可以大概知道改动了。如果懂些代码,可以深入到代码里面去看。或者拉着Dev一起看,确定最终范围。当然如果Dev的comments 写得够清楚,文件命名很规范,那就更好了。还可以辅助sourcecode 界面工具辅助查看。
这样,每次回归就不会那么没有底了。

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
# coding=utf-8
import subprocess
import sys
import urllib2
import time
import json
import re
import numpy
import matplotlib.pyplot as plt
from collections import Counter

__author__ = 'anderson'

reload(sys)
sys.setdefaultencoding('utf-8')

log_file = r'gitfiles.log'
start_time_stamp = time.strftime("%Y%m%d%H%M%S")

def write_log(file_name, content):
file_object = open(file_name, 'a+')
file_object.write(content)
file_object.close()


def get_comments(obj):
sentence = obj[obj.index(" "):].strip().rstrip("\n")
return sentence


def get_file(obj):
if obj.rfind("..."):
file = obj[obj.rindex("...") + 4:].strip().rstrip("\n")
return file


def get_git_info():
commit_comments = []
file_comments = []
commit_pattern = re.compile(r'^\d+\w+', re.IGNORECASE)
file_pattern = re.compile(r'^:\d+\w+', re.IGNORECASE)
cmd = "git whatchanged --since='2 weeks ago' --pretty=oneline"
lines = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
for line in lines:
if re.search(commit_pattern, line):
commit = get_comments(line)
commit_comments.append(commit)
write_log(log_file, commit)
write_log(log_file, "\n")
elif re.search(file_pattern, line):
commit_file = get_file(line)
file_comments.append(commit_file)
write_log(log_file, commit_file)
write_log(log_file, "\n")
return commit_comments, file_comments


def get_file_statistic(target):
table = Counter(target)
return table


def get_words_dict(target):
table = {}
line, number = re.subn(r"\W", " ", target)
for word in line.split():
if table.has_key(word):
table[word] += 1
else:
table[word] = 1
return table


def get_words_graphic(wordlist):
keylist = wordlist.keys()
vallist = wordlist.values()
barwidth = 0.3
xVal = numpy.arange(len(keylist))
plt.xticks(xVal + barwidth / 2.0, keylist, rotation=90)
plt.bar(xVal, vallist, width=barwidth, color='y')
plt.title(u'词频分析图')
plt.show()


def get_file_graphic(wordlist):
keylist = wordlist.keys()
vallist = wordlist.values()
barwidth = 0.3
xVal = numpy.arange(len(keylist))
plt.yticks(xVal + barwidth / 2.0, keylist, rotation=0)
plt.barh(xVal, vallist, height=barwidth, color='b')
plt.title(u'文件分析图')
plt.show()


def remove_dict(obj, key):
if key in obj.keys():
obj.pop(key)


if __name__ == '__main__':

write_log(log_file, start_time_stamp)
write_log(log_file, "\n")

comments, files = get_git_info()

remove_words = ['the', 'a', 'bug', 'to', 'of', 'so', 'one', 'more', 'we', 'ME', 'Update', 'app', 'our', 'issue',
'want', 't', 'work']

commit_str = ''.join(comments)
words_number = get_words_dict(commit_str)

for word in remove_words:
remove_dict(words_number, word)

fine_number = get_file_statistic(files)
get_words_graphic(words_number)
get_file_graphic(fine_number)