【问题标题】:py.test - How to inherit other testspy.test - 如何继承其他测试
【发布时间】:2016-02-18 15:31:23
【问题描述】:

假设我有两个文件(test_file1.pytest_file2.py)用于使用py.test 进行集成测试。

test_file1.py 是这样的:

import datetime
import pytest

Datetime = datetime.datetime.now()

def test_connect():

  #1st Query to a mysql database

  #2nd Query to a mysql database

  ..

  #N Query to a mysql database

现在我正在编写 test_file2.py,它是 test_file1.py 的扩展,但我不想编写与上述测试中相同的 mysql 查询。

我怎样才能让py.test继承上面的测试
并在执行py.test test_file2.py之后同时运行?

类似这样的东西(test_file2.py 内容):

import datetime
import pytest

from testDirectory import test_file1

Datetime = datetime.datetime.now()

def test_connect():

  #Here should run all the tests from 'test_file1' somehow...

  #1st new  additional Query to a mysql database

  #2nd new additional Query to a mysql database

  ..

  #N new additional Query to a mysql database

谢谢!!

【问题讨论】:

    标签: python unit-testing inheritance integration-testing pytest


    【解决方案1】:

    当您导入一个模块时,它将执行其中的所有代码。因此,只需在原始文件中编写您想要执行的代码。例如,在文件中添加对函数的调用,如下所示:

    test_file1.py:

    import datetime
    import pytest
    
    Datetime = datetime.datetime.now()
    
    def test_connect():
    
        #1st Query to a mysql database
    
        #2nd Query to a mysql database
    
        ..
    
        #N Query to a mysql database
    
    test_connect() # This will run your function when you import
    

    那么在你的py.test 中,当你调用import test_file1 时,它会执行test_connect() 和你想要的任何其他代码,而无需执行任何其他操作。

    换句话说,这是一个非常简单的示例,包含 3 个文件:

    文件 1:hello_world.py

    def hello_world():
        print('hello world!')
    
    hello_world()
    

    文件 2:print_text.py

    def print_text():
        print('foo bar baz')
    
    print_text()
    

    文件 3:run_everything.py

    import hello_world
    import print_text
    

    运行run_everything.py时的结果:

    >>>hello world!
    >>>foo bar baz
    

    如果你希望函数在文件直接执行时执行,而不是作为模块导入,你可以这样做:

    test_file1.py:

        import datetime
        import pytest
    
        Datetime = datetime.datetime.now()
    
        def test_connect():
    
           #1st Query to a mysql database
    
           #2nd Query to a mysql database
    
           ..
    
           #N Query to a mysql database
    
       def main():
           # This will _not_ run your function when you import. You would 
           # have to use test_file1.test_connect() in your py.test.
           test_connect()
    
    
       if __name__ == '__main__':
           main()
    

    所以在本例中,您的 py.test 将是:

        import test_file1
    
        test_file1.test_connect()
    

    【讨论】:

    • 确实!!那是真实的!!谢谢!!
    • 很高兴为您提供帮助。虽然在 'py.test' 中调用你想要的函数会更好。这样下次打开时,它在做什么就更清楚了。
    • 我刚注意到一个小问题,如果我将'test_file1.py'导入'test_file2.py'一切都很好,但如果我执行'py.test test_file1.py'然后'test_file1 .py' 被执行两次。
    • 你可以把你的函数调用放到一个函数中,这个函数只有在文件被执行时才会被调用,而不是被导入。但是,您必须在 py.test 中单独调用这些函数。我正在更新我的答案。
    【解决方案2】:

    第一个在conftest.py 中创建一个fixture:

    import pytest
    import MySQLdb
    
    def db_cursor(request):
        db = MySQLdb.connect(host="localhost", user="root")
        cursor = db.cursor()
        cursor.execute("SELECT USER()")
        data = cursor.fetchone()
        assert 'root@localhost' in data
        yield cursor
        db.close()
    

    然后在您的测试模块中使用它:

    # test_file1.py
    def test_a(db_cursor)
        pass
    
    # test_file2.py
    def test_b(db_cursor)
        res = db_cursor.execute("SELECT VERSION()")
        assert '5.5' in res.fetchone()
    

    附言

    可以使用任何其他模块,只需使用pytest_plugins 指令将它们注入到您的测试中:

    # conftest.py
    pytest_plugins = '_mysql.cursor'
    
    # _mysql/__init__.py
    
    # _mysql/cursor.py
    import pytest
    import MySQLdb
    
    def db_cursor(request):
        db = MySQLdb.connect(host="localhost", user="root")
        cursor = db.cursor()
        cursor.execute("SELECT USER()")
        data = cursor.fetchone()
        assert 'root@localhost' in data
        yield cursor
        db.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多