py.test - 如何继承其他测试
py.test - How to inherit other tests
假设我有两个文件(test_file1.py
、test_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
中创建夹具:
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()
假设我有两个文件(test_file1.py
、test_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
中创建夹具:
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()