py.test - 如何继承其他测试

时间:2016-02-18 15:31:24

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

因此,假设我有两个文件(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

谢谢!

2 个答案:

答案 0 :(得分:2)

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

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()

答案 1 :(得分:-1)

首先在conftest.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()

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

# 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()

P.S。

可以使用任何其他模块,只需使用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()