python之unittest

import unittest                               
from test import add,sub                      
def setUpModule():                            
    print("加载文件")                             
def tearDownModule():                         
    print("关闭文件")                             
class mytest(unittest.TestCase):              
    # @classmethod                            
    # def setUpClass(cls):                    
    #     print("环境搭建")                       
    # @classmethod                            
    # def tearDownClass(cls):                 
    #     print("环境销毁")                       
    def test_add(self):                       
        '''test add'''                        
        print("test_add")                     
        self.assertEqual(add(10,20),30)       
    def test_sub(self):                       
        '''test sub'''                        
        print("test_sub")                     
        self.assertEqual(sub(30,20),110)      
                                              
class myn(unittest.TestCase):                 
       # @classmethod                         
       # def setUpClass(cls):                 
       #     print("环境搭建")                    
       # @classmethod                         
       # def tearDownClass(cls):              
       #     print("环境销毁")                    
       def testdd(self):                      
        print("hdufhuwe")                     
                                              
if __name__=="__main__":                      
    unittest.main()                           
                                              
                                              

注:三种环境搭建,销毁

'''会在运行每一个TestCase前都创建一个环境,结束后进行销毁'''
def setUp(self):
    print("环境搭建")
def tearDown(self):
    print("环境销毁")
'''在运行每一个测试类时创建环境,在我销毁环境'''
def setUpClass(cls):
    print("环境搭建")
def tearDownClass(cls):
    print("环境销毁")
'''在运行一个模块前加载环境,整个模块运行完后销毁环境''''
def setUpModule():
    print("环境搭建")
def tearDownModule():
    print("环境销毁")
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、付费专栏及课程。

余额充值