繁体   English   中英

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

[英]py.test - How to inherit other tests

所以假设我有两个文件( 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

谢谢!!

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

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

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

PS

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM