一个菜鸟测试工程师的简易自动化测试框架
上一篇 / 下一篇 2012-09-07 11:24:05 / 个人分类:自动化测试
引子51Testing软件测试网m+h8tEYY-[
51Testing软件测试网&d/vX^.` ]作为一个无 dev 经验的 tester 来说,目前遇到许多新的问题,关于 tester 以后的人员基本素质的构成,现在也有许多说法,在这里,不想过多的铺开讨论了。 作为一个 tester,本身当然必须对test这件事情感兴趣,才能把工作做好。 有的人天生喜欢找茬,喜欢提意见,在我看来,就是一个不错的 tester 的料。 当你天天对着同一个产品的不同版本不停的测试的时候,自然会产生一些怨言,由此产生懈怠。51Testing软件测试网b_n.E4g+W-l
51Testing软件测试网,?LGWqek(zj“某些功能点我都连着测试了 N 个版本了,肯定不会出错了” “这种基本功能点,怎么可能出错”这样的论调是很有可能产生的。于是,一个 tester 很有可能在某次 release 的过程中,经验主义的放过了某些功能点的测试。 几天后,一个用户反馈上来,那个功能点出了问题.........
#n-vx#K1lx7`6Yh09_A,U#` r'@#J0 “偷懒”这个词语,在测试过程中,不应该算一个贬义词(当然了,故意漏过功能点不测试,不在此列)。因为“偷懒”催生新的技术,“偷懒”节省更多的时间。 我相信,自动化测试就是这样来的。51Testing软件测试网*] B-sP+ICT
-VCX.is0 正题
&S!P P:W a.kp2m @051Testing软件测试网"K9p\9[1b{yH }任何事情都可以分解为很小的部分,让我们先分析一下,手工执行测试案例的一个过程:
n S([(]&QUxg.c8Z051Testing软件测试网nJN?9F0QqXL M`打开被测软件
?;}2p3u*Z'_gm051Testing软件测试网#I.T+Pn%P&f1W1A执行测试案例
ao[$C&J^o6M0+D'AvW(G ~!`0 给出测试结果
4Ea_%Z!j^:Gq5dP051Testing软件测试网 Z?'FP{&]2sg3e然后,我们不断重复这个过程。 最后给出测试报告。51Testing软件测试网 \s*j"w TWO
Go*B1\"W!AhS0 显然,我们的自动化测试也要满足上面的这个过程中的所有要求。 既然是自动化,首先要求是所有的测试用例可以无人值守运行;其次,每个测试用例都由机器来模拟人对软件的操作;第三,某个测试用例失败不能影响其他测试用例的继续执行;第四,测试数据和测试用例最好分离开来;最后一点,测试结束之后,给个报告或者给个图告知测试结果。51Testing软件测试网C!e+S!M-}W"T
51Testing软件测试网b2L:@#vB根据上面的分析,我们逐个来一一解决,当然了,本篇都是很初级的解决方式,相信有许多大牛会有更好的方式来解决。
2L q;Sf+Z&f:K.n0s0a W'fE4a{;T!j+l*c0 无人值守运行,那么 python 的 unittest 的框架就能达到这个目的。51Testing软件测试网~ @,`:[6o;z^i&s X
51Testing软件测试网#^9[?j)f模拟人的操作, selenium 可以胜任。
l?KQ.y+Y051Testing软件测试网4oTd&F3K"{ W+F-Z:}*x测试用例间无影响, 同1
~+J#~ E[2aa0.hwn.m4a{^:Q0 数据分离, 那把测试数据都扔xml里。51Testing软件测试网qr a4L:@3m
H\S/O1z1{!|g,J0 待完善 (本例中 只是在部分方法中添加错误提示)51Testing软件测试网v|7ZVq!Q\l
51Testing软件测试网p,K W:i,s开始动手
} ]$`\$bE)Z|t0Q.~l2Zhv0|-[b0 对 python 的 unittest 进行一些小手术,当然了,如果只追求简单,直接用也行。51Testing软件测试网OpNR u \3V
def assertLogEqual(self, arg1, arg2):51Testing软件测试网j M@3n3@'rqprint "[assertLogEqual('"+arg1+"', '"+arg2+"')]: "
N}E|o^tF0if arg1 != arg2:51Testing软件测试网6y-dXH'Lk0`K7QB$U
self.anyfailure = True
cw?q:S0print " -> Failed: not equal"51Testing软件测试网w._\(fW&[ rn
self.result_note = " '%s' and 's%' is not equal\n "%(arg1,arg2)51Testing软件测试网3yD`M1_
else:
Z[P Mf` J%d0print " ->OK"51Testing软件测试网7uCakr6Wh
self.result_note = "%s OK\n" % self.result_note51Testing软件测试网'w(s ySd6` Uox
self.assertEqual(arg1, arg2)
7KY2n2U/PX2y0def assertLogTrue(self, arg1, note=""):
8Tg!x4\1T(H J0print "[assertLogTrue("+str(arg1)+")]: "+note51Testing软件测试网5m+|TA;~-S1AE4} V;C
if arg1 is False:
F Jd$XyD,s%c+F0self.anyfailure = True51Testing软件测试网v0m})i[Q
print " -> Failed: Excepting True"51Testing软件测试网Jba+Ba$t8t]
self.result_note = " %sError: %s\n " % (self.result_note, note)
v:M2T JOd-@D6}j8l0else:
|l$\(o]3C$tB+s0print " ->OK"51Testing软件测试网7HC)D%w r8a'kB7?
self.resulte_note = "%s OK\n" % self.result_note51Testing软件测试网NXT|)gV.ks6E?*N
self.assertTrue(arg1)51Testing软件测试网5f8y+}_HG#T.j!tt0^
def assertLogFalse(self, arg1, note=""):
h$f(`CNz} i"v0print "[assertLogFalse("+str(arg1)+")]: "+note51Testing软件测试网8K\/m GH
if arg1 is True:51Testing软件测试网oPe*V_"UO2W ^
print " ->FAILED: Expecting False."
5{&KK^]a ?y0self.anyfailure = True51Testing软件测试网`9L%|#wXA
self.result_note = "%sError: %s\n" % (self.result_note, note)
!QHnw$ro'_6a K\0else:51Testing软件测试网K\UOQ.RZy3R2A M
self.result_note = "%sOK\n" % self.result_note
z3T6`U)J L V#qI,H `0print " ->OK"51Testing软件测试网%EE!EgP$D
self.assertFalse(arg1)51Testing软件测试网~*h$T)S6L
S#a4{&jv_4RG f_9q0 自己新建一个类,继承unittest,然后修改必要的一些方法,并添加部分方法,譬如设置testName啊,设置标记值来标记case知否执行结束啊。这些都是可以根据自己喜好来加入的方法。
FI)jlE.T.AA/H051Testing软件测试网9E\'ctF2L5TM-_vQ?"h;H0def setTestName(self, tname):51Testing软件测试网.V'o7H;r^L\.b8u self.testname = tname 5\/O h7x${#t#pXQ0def getTestName(self): wx.l7Iq+R uzrL p0return self.testname V9XS xF rv;C'~te0def setCompleted(self):51Testing软件测试网1xoCe;V] self.completed = True51Testing软件测试网Ww#x4C[l)j)f/^0K9l def isCompleted(self): XUz6k4ZE6u"k+D o0return self.completed |
对于unittest框架的修改 就写这么多。51Testing软件测试网-jS o0n!olJ
51Testing软件测试网a}h)r x3l4~0mR对selenium的基本方法做一些小改动,动机优化 unittest 框架一致,只是为了让你的用例跑起来的时候更加让人理解。我们相当于在 selenium 的基本方法外面又封装了一层。下面是部分方法示例:51Testing软件测试网7{5WAI5Ir+kk
/w tn(~#U7v0b e&V;U N'U051Testing软件测试网5sX J,IA%pQ9O
def open(self,url): 4R*]#h-l|"F\2Q0self.sel.open(url) UxK*Z7O,cH3}y0def GetValue(self, element):51Testing软件测试网-drb(m4~6^}0@*_,M8y value = self.sel.get_value(element)51Testing软件测试网 UCn)Ht&]Mpj self.assertLogTrue(True, "Value for element %s is %s" %(element, value))51Testing软件测试网go8l,{8X"v'O return value Cv DP&EWbw.l0def isTextPresent(self, text): +l!vPVf4}]0val = self.sel.is_text_present(text) D,}#ehM,KoXF0if val is False: o b,T1q|.o7w0self.assertLogTrue(val, note="error occured *************" + text +\51Testing软件测试网$d8Q1T'C%e;Hi G1C*y " not found") Ix"J+X#[0else: {g{5nd(E"XH s0PD0print 'present (%s,%s)' % (text,val)51Testing软件测试网;Z5dWi n o7]+RV[N return val51Testing软件测试网+?7q$qWY|h i def Select(self, element, selection):51Testing软件测试网~!JG.G\f if self.isElementPresent(element):51Testing软件测试网g{:BZ/x+[9R self.sel.select(element, selection)51Testing软件测试网u Yv4Bk;Q Ci def Check(self, element):51Testing软件测试网Wr'N9QT if self.isElementPresent(element): Ag{DO9o0self.sel.check(element)51Testing软件测试网)KpIWf[sf def Type(self, element, text): -S/\o|pD0if self.isElementPresent(element): 2SEp0iC}0self.sel.type(element, text) |
测试数据分离,这个做的非常简单,就是把期望值和控件位置作为测试数据放入 xml 中。51Testing软件测试网US,f^ O*SP4X
(O4sv Bl(x6N0
.Wp5{VX:G F&e0GWEB51Testing软件测试网.VLA\!R4cy*_ Gd\ link kL{&y'Vd[2e/N:p k4[0//span[text()="Web"] sX5W!~I0Web k ~ z/?O0GIMAGES /TN.`HF+P'y5GD+b,b0link *j jZy;u;@wf/g,km0//span[text()="Images"]51Testing软件测试网#Tq-nx$N\ Images |
mA:~(s%x0 name 表示控件名字,type 为控件的类型,locator 表示控制位置,expect_value 表示期望值。
_"pTP @fm:u_0uFv;O6UCH9QJ [0D0 读取 xml 的内容。这段代码属于我现学现卖,直接看了一下 python 对 xml 的支持,然后自己捣鼓了一下就写出来了,可以获取自己需要的 xml 里面的数据。51Testing软件测试网6UuYC,GP
51Testing软件测试网~sd2tiOsl:aK*M8IZ51Testing软件测试网NSag]
from xml.dom import minidom V2@;qB1t c2AgD0class PageData():51Testing软件测试网y"| _`j*c def __init__(self, page_name, file):51Testing软件测试网R.Uyh1^9A9e&HX self.name = page_name A*l2qp._c9M@0self.data = minidom.parse(file) | zY'Y0n]8do#Q0self.xpth_dict = self.XML_Dict() 7Ng S4AGELz3_0def XML_Dict(self): tb%RN}#lDP0xpath_dict = {}51Testing软件测试网PAxa C%d"b for i in self.data.getElementsByTagName(self.name):51Testing软件测试网gmc/@? TL xpath_dict[str(i.childNodes[1].firstChild.nodeValue)] = \ V*f i!h SBn*N0[str(i.childNodes[5].firstChild.nodeValue),\ &E2vA,Z[W0str(i.childNodes[7].firstChild.nodeValue)] ej.lW f"G`%q6t9h5n ]0return xpath_dict51Testing软件测试网2NnAo%|%T _ def GetLocator(self, object_name):51Testing软件测试网 fN[E#tnl3q return self.xpth_dict[object_name][0] I5s$h8[ X%~ez'Z0def GetValue(self, object_name):51Testing软件测试网JMn.]'\(aGe7`ma(L return self.xpth_dict[object_name][1] |
最后,主程序里面,我们就能用下面的方式,执行我们本来已经设计好的案例。
D.Es;bH0&m:gG9}*g$x)pz051Testing软件测试网Na1NE\~#b
def testAutoCompleteFunctionMouseMove(self): N#r8e)nn0'''test the function of auto complete. case 2: when user move mouse to the suggestion, there will be a link ''' h8U G!bz E#C'sr0self.initTest("testAutoCompleteFunctionMouseMove") u,KJ G\w.O6}0self.open('http://www.google.com/ncr') od(d_ SWh0GoogleHomePage = data_parser.PageData("GoogleHomePage", self.data_file) dDL6j3k.F;n,Y0self.Type(GoogleHomePage.GetLocator('SEARCHTEXT'), 's') ][)S/e]X+H a5M&}0GoogleAutoComplete = data_parser.PageData("AutoCompleteCase", self.data_file) 0l/?M0N(nq:?:V ]j0self.isElementPresent(GoogleAutoComplete.GetLocator("SUGGESTIONFIELD"))51Testing软件测试网I"E-c1S${/]8o in a time.sleep(10) j{Z$N{q]&[9F)l0self.MouseMove(GoogleAutoComplete.GetLocator("SUGGESTIONONEFORS"))51Testing软件测试网lH.]@i1Q expect_text = "I'm Feeling Lucky ?" g!BEp5B2T"G_:I-Ti.Q0self.assertLogTrue(self.isTextPresent(expect_text), "The text %s has been displayed" %expect_text)51Testing软件测试网 PO;c(ssDR%@ self.MouseMove(GoogleAutoComplete.GetLocator("SUGGESTIONTWOFORS")) ~5M(Mif.~ v$Hh0time.sleep(10) qa9WA3Ty._ G0self.assertLogTrue(self.isTextPresent(expect_text), "The text %s has been displayed" %expect_text)51Testing软件测试网 Ncm g] self.endTest() |
xh/ps^}S]0W0 这样,一个相当简易的自动化测试框架基本完工。可以完善的地方实在太多了,希望有志之士去完善吧,加入些新功能,譬如错误出现的时候截图,然后统计测试用例总数以及通过的数量。
`H?GD3jYxIj%}!c0@#t2I_aEz8T3g0 后记51Testing软件测试网6eI6NMKDlk
51Testing软件测试网)z&F,W:d[作为一个菜鸟测试工程师,没有任何的开发经验,搞出这个东西还是有点小激动的,也激发了本人对许多事情的兴趣。 以后希望能有更好的 test frame. 去学习去创造。
I4kh3a&_"?051Testing软件测试网n;]#Uc){1Oc,RTAG:
不要让那些真正对你好的人,慢慢的从你的生活中消失,无论爱情还是友情,都需要用心经营。
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
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 |
我的存档
数据统计
- 访问量: 3343502
- 日志数: 1640
- 建立时间: 2011-12-07
- 更新时间: 2019-12-24