当前位置:网站首页>Ddt+yaml implementation of data driven mechanism based on unittest
Ddt+yaml implementation of data driven mechanism based on unittest
2022-07-28 00:46:00 【Si Xiaoyou】
Catalog
1.data.txt
account number , password
2.demo.yaml
-
name: xuzhu
age: '14'
sexual: male
hobby:
- Locomotive
- Thai Boxing
- Boxing
address:
Changsha : xxx
# city in Hunan : xxx
3.login.yaml
-
# Anchor point
# common: &defaults
common:
url: http://39.98.138.157/shopxo/index.php?s=/index/user/logininfo.html
accounts:
by: name
value: accounts
password:
by: name
value: pwd
button:
by: xpath
value: //button[text()=" Sign in "]
assert_text:
expected: sign out
by: link text
value: sign out
sleep: 5
username: sixiaoyou
pwd: xxx
#-
# common:
# <<: *defaults
# xuzhu: sssss
# username: xuzhu666
# pwd: xxx
4.search.yaml
-
url: http://39.98.138.157/shopxo/index.php
input:
by: id
value: search-input
txt: mobile phone
click:
by: id
value: ai-topsearch
sleep: 5
5. Read yaml file
import yaml
file = open('./data/login.yaml','r',encoding = 'utf-8')
values = yaml.load(stream = file,Loader = yaml.FullLoader)
for value in values:
print(value)
6.DDT Use
''' Unittest Automatic test implementation under A complete process , Different small processes are divided into different use cases to manage . This is easy to maintain DDT Use ( Don't pass all the data directly @data Decorator for transmission ): Use through decorator form 1. @data Data driven implementation of decorator : Import relatively simple or fixed data content [email protected] Decorator : take data Disassemble the data twice , It is convenient to meet the situation that multiple parameters are passed in during the execution of a single use case [email protected]_data() Decorator , obtain yaml File content in format 4. stay setup and teardown in , Will not import ddt Any decorator to do data-driven Include setupclass\teardownclass @data([' Xiaoyou should work hard !',' Yunsha Liying should learn !']) What you get after disassembling is a list, If you want to list Split twice , You need to add @unpack Decorator unpack:[' Xiaoyou should work hard !',' Yunsha Liying should learn !'] The second is based on , To break up get ' Xiaoyou should work hard !',' Yunsha Liying should learn !' Two figures '''
import unittest
from ddt import ddt, data, unpack, file_data
from class24.web_keys import Keys
1. Get file data content
def read_file():
values = list()
file = open('./data/data.txt', 'r', encoding='utf-8')
for line in file.readlines():
values.append(line)
return values
2. call ddt Data driven
# Must be in class Add ddt The decorator
@ddt
class UnitAuto(unittest.TestCase):
# Back and forth
@classmethod
def setUpClass(cls) -> None:
cls.key = Keys('Chrome')
# def setUp(self) -> None:
# self.key = Keys('Chrome')
# def tearDown(self) -> None:
# self.key.quit()
@classmethod
def tearDownClass(cls) -> None:
cls.key.quit()
# Login test cases
# def test_01_login(self):
# key = Keys('Chrome')
# self.key.open('http://39.98.138.157/shopxo/index.php?s=/index/user/logininfo.html')
# self.key.input('name','accounts','sixiaoyou')
# self.key.input('name','pwd','xxx')
# self.key.click('xpath','//button[text()=" Sign in "]')
# self.assertEqual() # Be similar to assert a == b,msg
# self.assertTrue(self.key.assert_text('link text',' sign out ',' sign out 1'),msg = ' Assertion failed ')
# self.assertEqual(' sign out 1',self.key.get_text('link text',' sign out '),msg = ' Assertion failed ')
# self.key.sleep(5)
# key.quit()
# @file_data('./data/login.yaml')
# def test_01_login(self,**kwargs):
# self.key.open(kwargs['url'])
# self.key.input(**kwargs['accounts'])
# self.key.input(**kwargs['password'])
# self.key.click(**kwargs['button'])
# self.assertEqual(kwargs['assert_text']['expected'],self.key.get_text(kwargs['assert_text']['by'],kwargs['assert_text']['value']),msg=' Assertion failed ')
# self.key.sleep(kwargs['sleep'])
@file_data('./data/login.yaml')
def test_01_login(self, **kwargs):
data = kwargs['common']
self.key.open(data['url'])
self.key.input(**data['accounts'],txt = kwargs['username'])
self.key.input(**data['password'],txt = kwargs['pwd'])
self.key.click(**data['button'])
self.assertEqual(data['assert_text']['expected'],
self.key.get_text(data['assert_text']['by'], data['assert_text']['value']), msg=' Assertion failed ')
self.key.sleep(data['sleep'])
# def test_02_search(self):
# key = Keys('Chrome')
# self.key.open('http://39.98.138.157/shopxo/index.php')
# self.key.input('id','search-input',' mobile phone ')
# self.key.click('id','ai-topsearch')
# self.key.sleep(5)
# key.quit()
# @file_data('./data/search.yaml')
# def test_02_search(self,**kwargs):
# self.key.open(kwargs['url'])
# self.key.input(**kwargs['input'])
# self.key.click(**kwargs['click'])
# self.key.sleep(kwargs['sleep'])
[email protected] Data driven implementation mechanism
# Import external data into test cases
''' @data Data driven implementation mechanism : @data(’ Xiaoyou should work hard !',' Yunsha Liying should learn !') be based on , Segmentation , Split into two different sets of strings , Based on how many groups of data are split , How many times will this example be executed String from data Get from decorator , Then it is passed to the test case as a parameter '''
# @data([' Xiaoyou should work hard !', ' Yunsha Liying should learn !'], [' Duanyu is very stingy ', ' Zhiruo is fierce '])
# @unpack
# def test_01(self, b, a):
# print(' These are two parameters :{} and {}'.format(a, b))
# data Decorators can call functions
# @unpack
# @data(*read_file())
# @unpack
# def test_02(self,a):
# # print(a+' as well as '+b)
# print(a)
# name = xuzhu
# age = 14
# @file_data('./data/demo.yaml')
# def test_02(self,**kwargs):
# # print(' Xu Zhu's identity was revealed : ' + str(age)+' and '+str(name))
# print(kwargs['address'])
if __name__ == '__main__':
unittest.main()
边栏推荐
- startUMl
- The latest notice of the Chinese Academy of Sciences: abandon the impact factor! The journal zoning table will be published for the "Journal surpassing index"
- "Digital economy, science and technology for the good" talk about dry goods
- Point divide and conquer analysis
- FFT 采样频率和采样点数的选取
- Solve maze problem recursively
- ADB path cannot contain 2 spaces remote could n't create file: is a directory
- 强强协同,共拓发展!英特尔与太一物联举办 AI 计算盒聚合服务研讨会
- Leetcode 452. minimum number of arrows to burst balloons (medium)
- Build Release Blogs
猜你喜欢

Matlab | matlab terrain generation: rectangular iteration method, inverse Fourier transform method, fractal Berlin noise method

阿里二面:为什么要分库分表?

Data analysis: disassembly method (details)

mysql分表之后怎么平滑上线?

Solve maze problem recursively

Set 数据构造函数

Buildforge materials

Data visualization - White Snake 2: black snake robbery (3)

Intel joins hands with hanshuo and Microsoft to release the "Ai + retail" trick!

mysql数据库的基本操作(一)-——基于数据库
随机推荐
单片机之led、数码管与按键
startUMl
Data analysis: disassembly method (details)
How to realize fast recognition of oversized images
Matlab | those matlab tips you have to know (3)
What has the metauniverse of more than 30 years brought to us?
CSDN21天学习挑战赛
网络设备硬核技术内幕 防火墙与安全网关篇 (十一) 零接触办公的奥秘 上
LeetCode_ Bit operation_ Medium_ 137. Number II that appears only once
Firefox 103, the Firefox browser, has been released to improve performance under high refresh rate displays
FFT 采样频率和采样点数的选取
The latest notice of the Chinese Academy of Sciences: abandon the impact factor! The journal zoning table will be published for the "Journal surpassing index"
The Canadian court found Meng Wanzhou guilty of "dual criminality", and the extradition procedure will continue!
网络设备硬核技术内幕 防火墙与安全网关篇 (十)
Intel AI practice day issue 56 | explore new trends in industry development
Application scenario Display of metauniverse
网络设备硬核技术内幕 防火墙与安全网关篇 (小结)
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
"Digital economy, science and technology for the good" talk about dry goods
Buildforge materials