【问题标题】:Call pytest from a python script with an argument从带有参数的 python 脚本调用 pytest
【发布时间】:2020-05-17 05:00:27
【问题描述】:

项目结构:

/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 的测试方法?

【问题讨论】:

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


    【解决方案1】:

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

    testing.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)
    

    这对您有帮助吗? :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 2018-12-27
      • 2019-11-03
      • 1970-01-01
      相关资源
      最近更新 更多