目錄
- 一、os
- 二、configparser
- 三、openpyxl
- 四、loguru
- 五、time
- 六、unittest
一、os
__file__
獲取當(dāng)前運行的.py文件所在的路徑(D:\PycharmProjects\My_WEB_UI\ConfigFiles\ConfigPath.py)
os.path.dirname(__file__)
上面正在運行的.py文件的上一級(D:\PycharmProjects\My_WEB_UI\ConfigFiles)
os.path.join(xxx,u'ConfigFiles\elementLocation.ini')
在已獲得的路徑xxx上加上\ConfigFiles\elementLocation.ini
二、configparser
config = configparser.ConfigParser()
創(chuàng)建一個configparser對象
config.read(filename)
讀取ini文件,filename為ini文件的路徑
config.sections()
得到ini文件內(nèi)的所有的section,以列表的形式返回
config.items(sectionName)
根據(jù)section的name得到其下的所有鍵值對,再用dict(config.items(sectionName))封裝為字典形式
三、openpyxl
wb = load_workbook('a.xlsx')
讀取文件a.xlsx
sheet = wb[sheetname]
根據(jù)名字拿到xlsx文件里對應(yīng)的頁
sheet.max_row
獲取當(dāng)前頁的最大行數(shù)
sheet.max_column
獲取當(dāng)前頁的最大列數(shù)
sheet.cell(row = xxx,column = xxx).value
獲取單元格(xxx,xxx)中的值
sheet.cell(row = xxx,column = xxx).value =aa #修改單元格里的值
wb.save(a.xlsx的路徑名) #修改完要保存一下,否則修改不生效
openpyxl庫中沒有方法來獲取去某一行的值,可以自定義:
row_data = []
for i in range(1,sheet.max_column+1): #注意遍歷列的時候從1開始
cell_value = sheet.cell(row = xxx,column = i).value #xxx就為具體想要獲取的行
row_data.append(cell_value)
四、loguru
logger.debug('this is a debug message')
logger.info('this is a info message')
logger.warning('this is a warning message')
logger.error('this is a error message')
logger.success('this is a success message')
logger.critical('this is a critical message')

logger.add('xxx.log')
在當(dāng)前同級目錄下創(chuàng)建一個xxx.log文件,并將接下來的日志打印到xxx.log里面
logger.add('lowPath/xxx.log')
在當(dāng)前目錄下創(chuàng)建一個文件夾lowPath,在其中創(chuàng)建xxx.log文件
logger.add(otherPath+'/xxx.log')
事先獲取其他的目錄otherPath,在otherPath下創(chuàng)建xxx.log
五、time
time.sleep(2)
強制休眠兩秒
time.strftime('%Y-%m-%d_%H-%M-%S')
接受當(dāng)前時間元組,并最終返回對應(yīng)格式的字符串
六、unittest
基本概念:
testcase 測試用例,以test開頭,執(zhí)行順序會按照方法名的ASCII碼值來排序
test suite 測試套件,testloader把需要一起執(zhí)行的測試用例加載到套件中,然后一起執(zhí)行
test runner 執(zhí)行測試用例并返回測試結(jié)果
test fixture 測試固件,對一個測試用例環(huán)境的搭建和銷毀
常見斷言: assertEqual(a,b,msg=None) 判斷a和b是否相等 assertNotEqual assertTrue(a)
判斷a是否為True assertFalse assertIs(a,b)
判斷a is b assertIsNot assertIsNone(a)
判斷a is None assertIsNotNone assertIn(a,b)
判斷a in b assertNotIn assertIsInstance(a,b)
判斷a是不是b的實例 assertIsNotInstance 斷言失敗會報AssertionError的錯
編寫測試用例
class TestDemo(unittest.TestCase):
繼承unittest模塊里的TestCase
def setUp(self)
準(zhǔn)備環(huán)境,執(zhí)行測試用例的前置條件
def tearDown(self)
環(huán)境還原,執(zhí)行測試用例的后置條件
def test_01(self)
測試用例1
if __name__ == '__main__':
unittest.main()
執(zhí)行當(dāng)前文件以test開頭的測試用例
########################################以下是實例##############################################
import time
import unittest
from selenium import webdriver
from Modules.LoginAction import LoginAction
class Login_test(unittest.TestCase):
def setUp(self):
'''
準(zhǔn)備好環(huán)境,執(zhí)行測試用例的前置條件
:return:
'''
self.driver = webdriver.Chrome()
self.driver.get('https://mail.163.com/')
self.driver.maximize_window()
def tearDown(self):
time.sleep(2)
self.driver.quit()
def test_01(self):
loginAction = LoginAction()
loginAction.do_login(self.driver, 'lsqtester001', 'qwer123')
time.sleep(2)
self.assertIn('lsqtester002',self.driver.page_source)
if __name__ == '__main__':
unittest.main()
組織測試用例
suit = unittest.TestSuite()
定義一個測試套件
suit.addTest(Login_test('test_01'))
向套件中添加測試用例
runner = unittest.TextTestRunner()
runner.run(suit)
定義testrunner并執(zhí)行已加入測試套件的測試用例
loader = unittest.TestLoader()
定義一個testloader對象
suit.addTest(loader.discover(TestcasesPath,pattern='Unittest*.py'))
根據(jù)條件將測試用例加載到套件中
########################################以下是實例##############################################
import unittest
from ConfigFiles.ConfigPath import TestcasesPath
from TestCases.Unittest_login import Login_test
if __name__ == '__main__':
# suit = unittest.TestSuite()
# #向套件中添加測試用例
# suit.addTest(Login_test('test_01'))
# suit.addTest(Login_test('test_02'))
#
# runner = unittest.TextTestRunner()
# runner.run(suit)
'''
用discover來組織測試用例
discover(dir,pattern='Unittest*.py',top_level_dir=None)
dir就是存放寫用例的python文件的具體路徑
pattern就是在目錄dir下找形式如同Unittest*.py這樣的文件
如果符合條件的.py文件里有l(wèi)oad_test這個函數(shù)的話,就會加載該文件里的測試用例
如果不存在load_test函數(shù)的話,就會默認(rèn)加載文件里以test開頭的測試用例函數(shù)
'''
suit = unittest.TestSuite()
loader = unittest.TestLoader()
suit.addTest(loader.discover(TestcasesPath,pattern='Unittest*.py'))
runner = unittest.TextTestRunner()
runner.run(suit)
到此這篇關(guān)于Python基礎(chǔ)之常用庫常用方法整理的文章就介紹到這了,更多相關(guān)Python常用庫常用方法整理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python編程開發(fā)之textwrap文本樣式處理技巧
- Python的文本常量與字符串模板之string庫
- Python中使用subprocess庫創(chuàng)建附加進(jìn)程
- Python超簡單容易上手的畫圖工具庫推薦
- python爬蟲請求庫httpx和parsel解析庫的使用測評
- Python高級文件操作之shutil庫詳解
- Python超簡單容易上手的畫圖工具庫(適合新手)
- python學(xué)習(xí)之panda數(shù)據(jù)分析核心支持庫
- Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫
- Python繪圖庫Matplotlib的基本用法
- Python爬蟲爬取愛奇藝電影片庫首頁的實例代碼
- Python Excel處理庫openpyxl詳解
- python使用openpyxl庫讀寫Excel表格的方法(增刪改查操作)
- Python time庫的時間時鐘處理
- python數(shù)據(jù)庫批量插入數(shù)據(jù)的實現(xiàn)(executemany的使用)
- Python爬蟲之必備chardet庫
- python中requests庫+xpath+lxml簡單使用
- Python格式化文本段落之textwrap庫