python中yaml传参
时间: 2023-09-29 18:03:24 浏览: 141
在Python中,可以使用PyYAML库来解析和生成YAML格式的数据。使用PyYAML库传递参数的一般步骤如下:
1. 安装PyYAML库:
```python
pip install PyYAML
```
2. 导入PyYAML库:
```python
import yaml
```
3. 读取YAML文件:
```python
with open('file.yaml', 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
```
4. 传递参数:
```python
def my_function(param1, param2):
# do something with param1 and param2
pass
my_function(data['param1'], data['param2'])
```
在YAML文件中,参数可以按照字典的方式进行存储,例如:
```yaml
param1: value1
param2: value2
```
这样,就可以通过读取YAML文件来传递参数。
相关问题
用pytest+pom模式写一个登录页面的case,用yaml传参username和password
当你使用Python的测试框架Pytest结合Page Object Model (POM)模式编写登录页面的测试用例时,首先需要创建两个关键模块:`LoginPage`(代表登录页面的对象)和`test_login.py`(存放测试用例)。假设你的项目结构已经包含这两个文件。
`LoginPage.py`(Page Object):
```python
from selenium.webdriver import WebElement
from selenium.webdriver.common.by import By
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username_input = (By.ID, "username")
self.password_input = (By.ID, "password")
self.login_button = (By.XPATH, "//button[@type='submit']")
def enter_username(self, username):
self.driver.find_element(*self.username_input).send_keys(username)
def enter_password(self, password):
self.driver.find_element(*self.password_input).send_keys(password)
def submit_login(self):
self.driver.find_element(*self.login_button).click()
```
`test_login.py`(Test Case):
```python
import pytest
from page_objects.login_page import LoginPage
@pytest.mark.parametrize("params", [
{"username": "admin", "password": "123456"},
{"username": "user", "password": "pass123"},
])
def test_login(params):
# 创建一个新的浏览器会话
with pytest.live_server_url("http://localhost:8000"):
login_page = LoginPage(webdriver) # 假设webdriver是一个初始化好的Selenium WebDriver实例
login_page.open() # 打开登录页面
login_page.enter_username(params["username"])
login_page.enter_password(params["password"])
login_page.submit_login()
# 验证登录成功后的操作,这里省略,例如检查是否跳转到首页
assert True, f"Login failed for {params['username']}"
# 使用YAML参数化
params_file = "login_params.yaml"
with open(params_file, 'r') as file:
param_data = yaml.safe_load(file)
for data in param_data:
test_login(data)
```
在这个例子中,`test_login.py`通过`@pytest.mark.parametrize`装饰器从名为`login_params.yaml`的外部YAML文件读取username和password的组合作为参数。每个测试用例都会自动运行一次,每次传入YAML文件中的一组参数。
`login_params.yaml`示例内容:
```yaml
---
- username: admin
password: 123456
- username: user
password: pass123
```
在pytest中使用yaml传参,yaml中的内容为- data_json1: { data_json: { "dataConllection": '{"dataBMR":1186,"dataBirthMonth":6,"dataBirthYear":2000,"dataBreakfastRNI":"325~458","dataDbz":"52","dataDbzRate":"0.16","dataDinnerRNI":"325~458","dataGender":"female","dataHeight":"160","dataId":"","dataLunchRNI":"459~585","dataRDI":1305,"dataSnacksRNI":"0~131","dataSportState":0,"dataTs":"173","dataTsRate":"0.53","dataUserId":"ac118362914749b7a57b01d925e35dad","dataUserWeightValue":"100.0","dataZf":"36","dataZfRate":"0.31","associatedModelsMapForJoinTable":{},"associatedModelsMapWithFK":{},"associatedModelsMapWithoutFK":{},"baseObjId":1,"fieldsToSetToDefault":null,"listToClearAssociatedFK":[],"listToClearSelfFK":[]}' } },怎么修改才能让这个只成为一个用例
你可以将 YAML 文件的内容修改为以下形式:
```yaml
- name: test_case_name
data_json: '{"dataBMR":1186,"dataBirthMonth":6,"dataBirthYear":2000,"dataBreakfastRNI":"325~458","dataDbz":"52","dataDbzRate":"0.16","dataDinnerRNI":"325~458","dataGender":"female","dataHeight":"160","dataId":"","dataLunchRNI":"459~585","dataRDI":1305,"dataSnacksRNI":"0~131","dataSportState":0,"dataTs":"173","dataTsRate":"0.53","dataUserId":"ac118362914749b7a57b01d925e35dad","dataUserWeightValue":"100.0","dataZf":"36","dataZfRate":"0.31","associatedModelsMapForJoinTable":{},"associatedModelsMapWithFK":{},"associatedModelsMapWithoutFK":{},"baseObjId":1,"fieldsToSetToDefault":null,"listToClearAssociatedFK":[],"listToClearSelfFK":[]}'
```
这样你就可以在 pytest 中使用 `data_json` 变量来引用该字符串了。此外,你还需要在 pytest 的测试函数中接收这个参数,例如:
```python
def test_my_function(data_json):
# 使用 data_json 参数
pass
```
阅读全文