python nosetests的作用和原理

如题

如何使用Python和Nose实现自动化测试?
本文我将详细介绍使用Appium下的Python编写的测试的例子代码对一个iOS的样例应用进行测试所涉及的各个步骤,而对Android应用进行测试所需的步骤与此非常类似。
然后按照安装指南,在你的机器上安装好Appium。
我还需要安装Appium的所有依赖并对样例apps进行编译。在Appium的工作目录下运行下列命令即可完成此任务:
$ ./reset.sh --ios
编译完成后,就可以运行下面的命令启动Appium了:
$ grunt appium
现在,Appium已经运行起来了,然后就切换当前目录到sample-code/examples/python。接着使用pip命令安装所有依赖库(如果不是在虚拟环境virtualenv之下,你就需要使用sudo命令):
$ pip install -r requirements.txt
接下来运行样例测试:
$ nosetests simple.py
既然安装完所需软件并运行了测试代码,大致了解了Appium的工作过程,现在让我们进一步详细看看刚才运行的样例测试代码。该测试先是启动了样例应用,然后在几个输入框中填写了一些内容,最后对运行结果和所期望的结果进行了比对。首先,我们创建了测试类及其setUp方法:
classTestSequenceFunctions(unittest.TestCase):

defsetUp(self):
app=os.path.join(os.path.dirname(__file__),
'../../apps/TestApp/build/Release-iphonesimulator',
'TestApp.app')
app=os.path.abspath(app)
self.driver=webdriver.Remote(
command_executor='127.0.0.1:4723/wd/hub',
desired_capabilities={
'browserName':'iOS',
'platform':'Mac',
'version':'6.0',
'app': app
})
self._values=[]
“desired_capabilities”参数用来指定运行平台(iOS 6.0)以及我们想测试的应用。接下来我们还添加了一个tearDown方法,在每个测试完成后发送了退出命令:
deftearDown(self):
self.driver.quit()
最后,我们定义了用于填写form的辅助方法和主测试方法:
def_populate(self):
# populate text fields with two random number
elems=self.driver.find_elements_by_tag_name('textField')
foreleminelems:
rndNum=randint(0,10)
elem.send_keys(rndNum)
self._values.append(rndNum)
deftest_ui_computation(self):
# populate text fields with values
self._populate()
# trigger computation by using the button
buttons=self.driver.find_elements_by_tag_name("button")
buttons[0].click()
# is sum equal ?
texts=self.driver.find_elements_by_tag_name("staticText")
self.assertEqual(int(texts[0].text),self._values[0]+self._values[1])
温馨提示:答案为网友推荐,仅供参考