簡體   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