Python之unittest

          unittest的基本使用方法

1.import unittest
2.定义一个继承自unittest.TestCase的测试用例类
3.定义setUp和tearDown,在每个测试用例前后做一些辅助工作。
4.定义测试用例,名字以test开头。
5.一个测试用例应该只测试一个方面,测试目的和测试内容应很明确。主要是调用assertEqual、assertRaises等断言方法判断程序执行结果和预期值是否相符。
6.调用unittest.main()启动测试
7.如果测试未通过,会输出相应的错误提示。如果测试全部通过则不显示任何东西,这时可以添加-v参数显示详细信息。


Unittest断言

作用:期望结果与实际结果比较,来判断该case是通过还是失败
但是不影响其他的case执行。




import unittest
import HTMLTestRunner

class Test002(unittest.TestCase):
#before run test
    def setUp(self):
        print 'Case Before'
        pass

#after run test
    def tearDown(self):
        print 'Case After'
        pass

    #test 1
    def test_Case1(self):
        a = 3
        b = 2
        self.assertEqual(a+b,6,'test_case1 is fail.')

        print "test_case1 is successfule."
    #test 2
    def test_Case2(self):
        a = 2
        b = 3
        self.assertEqual(a*b,6,'test_case2 is fail.')
        print "test_case2 is successfule."

    #test 3
    def test_Case3(self):
        a = 5
        b = 3
        self.assertEqual(a-b,2,'test_case2 is fail.')
        print "test_case2 is successfule."

if __name__ == '__main__':
    #unittest.main()
    suite=unittest.TestSuite()#define test suite
    suite.addTest(Test002('test_Case1'))
    suite.addTest(Test002('test_Case2'))
    suite.addTest(Test002('test_Case3'))
    #unittest.TextTestRunner(verbosity=2).run(suite)

    outfile = open("Report.html", "w")
    runner = HTMLTestRunner.HTMLTestRunner(
                stream=outfile,
                title='Test Report',
                description='test one.'
                )
    runner.run(suite)


Python Appium unittest is a testing framework used to automate mobile application testing using Python programming language. It is built on top of the unittest module and provides a set of methods and assertions to test mobile applications. To use Python Appium unittest, you need to have Appium installed on your machine and a mobile device or emulator to test your application on. You also need to have the Appium Python Client installed in your Python environment. Here is an example of a Python Appium unittest script: ``` import unittest from appium import webdriver class TestApp(unittest.TestCase): def setUp(self): desired_caps = { 'platformName': 'Android', 'deviceName': 'Android Emulator', 'appPackage': 'com.example.myapp', 'appActivity': 'MainActivity' } self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self): self.driver.quit() def test_login(self): email_field = self.driver.find_element_by_id('email') email_field.send_keys('testuser@example.com') password_field = self.driver.find_element_by_id('password') password_field.send_keys('password123') login_button = self.driver.find_element_by_id('login_button') login_button.click() assert 'Welcome' in self.driver.page_source if __name__ == '__main__': unittest.main() ``` In this example, we are testing a login functionality of an Android application. We first set up the desired capabilities for our test, which include the platform name, device name, app package, and app activity. We then initialize the Appium webdriver with these capabilities. In the `test_login` method, we find the email and password fields and enter test data. We then find the login button and click on it. Finally, we assert that the word 'Welcome' is present in the page source after successful login. To run this test, you can simply execute the script in your terminal using the command `python test_app.py`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值