python - 从带有参数的 python 脚本调用 pytest

标签 python python-3.x pytest python-3.7 python-dataclasses

项目结构:

/prj_root
   |
   |___some_test.py
   |
   |___main.py

我在 pytest 中编写了以下测试:

some_test.py:

def test_something(entty):
    #Some pytest code

和以下脚本

main.py:

@dataclass
class Entity(object):
    name: str

if __name__=='__main__':
    entity = Entity(name = 'some_stub_value')
    #Here I want to invoke pytest's test_something test method
    #with entity passed as an argument

有没有办法从 python 脚本将参数传递给 pytest 的测试方法?

最佳答案

我只是混合了一些东西并尝试了这个:

测试.py

def test_something(entty):
    print(entty.name)

testing2.py

from testing import test_something

class Entity(object):
    def __init__(self, name):
        self.name = name

if __name__=='__main__':
    entity = Entity(name = 'some_stub_value')
    test_something(entity)

这对你有帮助吗? :)

关于python - 从带有参数的 python 脚本调用 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60005337/

相关文章:

python - 如何在 Python 中仅使用 "A-Z","a-z","0-9"和 "_"对 UTF-8 字符串进行编码

python - 在 python selenium 中,如何找到元素的可见性?

python - 如何使用正则表达式或索引从非日期列派生日期?

python - 继续在 python 中创建可能的字符串列表

python - Python 中的正则表达式 : Replace starting and closing brackets with dynamic/incremental strings

python - 使用 lxml/ElementTree 获取非连续文本

python - py.test : Temporary folder for the session scope

mongodb - 如何用MongoDB(Motor)实现FastAPI的pytest

python - 当灯具具有相同参数时如何使用间接参数化?

python - Keras model.summary() 结果 - 了解参数的数量