[英]py.test - How to inherit other tests
So let's say that I have two files ( test_file1.py
, test_file2.py
) for integration testing using py.test
. 所以假设我有两个文件(
test_file1.py
, test_file2.py
)用于使用py.test
进行集成测试。
The test_file1.py
is something like this: 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
Now I'm writing the test_file2.py
which is an extention of test_file1.py
but I don't want to write the same mysql queries that I wrote in the above test. 现在我正在写的
test_file2.py
这是一个推广test_file1.py
但我不想写,我在上面写测试相同的MySQL查询。
How can I make py.test
to inherit the above test 如何让
py.test
继承上面的测试
and run both after executing py.test test_file2.py
? 并在执行
py.test test_file2.py
之后运行它们?
Something like this ( test_file2.py
Contents): 像这样的东西(
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
Thanks!! 谢谢!!
When you import a module, it will execute all of the code inside it. 导入模块时,它将执行其中的所有代码。 So just write the code you want executed in your original file.
所以只需编写您想要在原始文件中执行的代码。 For example add the call to the function in your file like this:
例如,在文件中添加对函数的调用,如下所示:
test_file1.py
: 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
So then in your py.test
when you call import test_file1
, it will execute the test_connect()
and any other code you would like without doing anything else. 所以在你那么
py.test
当你打电话import test_file1
,它将执行test_connect()
和任何其他代码,你想没有做其他任何事情。
In other words, here is a really simple example with 3 files: 换句话说,这是一个包含3个文件的简单示例:
File 1: hello_world.py
: 文件1:
hello_world.py
:
def hello_world():
print('hello world!')
hello_world()
File 2: print_text.py
: 文件2:
print_text.py
:
def print_text():
print('foo bar baz')
print_text()
File 3: run_everything.py
: 文件3:
run_everything.py
:
import hello_world
import print_text
Result when you run run_everything.py
: 运行
run_everything.py
时的结果:
>>>hello world!
>>>foo bar baz
If you want the function to be executed when the file is executed directly, but not imported as a module, you can do this: 如果希望在直接执行文件时执行该函数,而不是作为模块导入,则可以执行以下操作:
test_file1.py
: 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()
So in this example, your py.test
would be: 所以在这个例子中,你的
py.test
将是:
import test_file1
test_file1.test_connect()
First one create a fixture in conftest.py
: 首先在
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()
Then use it in your test modules: 然后在测试模块中使用它:
# 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()
It possible to use any other modules, just inject they are into your tests with pytest_plugins
directive: 可以使用任何其他模块,只需使用
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.