0
Follow
2
View

How to use functions in yaml files in pytest framework;

djzhao76 注册会员
2023-02-27 10:15

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > in the frame of the pytest, You can use Python's yaml library, PyYAML, to read and parse yaml files, allowing you to pass arguments using functions as variables.

First, you need to import the PyYAML library into Python:
import yaml
You can then read the yaml file and parse it into a Python object using the yaml.load() method:
with open('example.yaml', 'r') as f:
    data = yaml.load(f, Loader=yaml.FullLoader)


Suppose a yaml file defines the following:

name: John
age: 30
is_adult: !!python/object/apply:builtins.bool [!!python/function:__import__ ["builtins"], 'True']


where the value for is_adult is a Python bool, initialized using the builts.bool() function.

Using the values in this yaml file in Python gives you direct access to the corresponding properties in the parsed Python object. For example:

name = data['name']
age = data['age']
is_adult = data['is_adult']


This allows you to use variables from yaml files in pytest test cases.

About the Author

Question Info

Publish Time
2023-02-27 10:15
Update Time
2023-02-27 10:15

Related Question

stringr函数的奇怪答案

flutter Bad state: Stream has already been listened to.

报错:SyntaxError: invalid syntax

Linux学习路线详细介绍

解码base64url为uint8array

如何在python中使用tkinter更改某些字符的颜色?

IntelliJ Idea2022.2.1生成一个spring boot项目

求一个简单的sql语句,关于多表join的

jenkinsfile 设置MR自动触发器不生效,怎么解决呀

springMVC入门 tomat不存在无法激活