简体   繁体   中英

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 .

The test_file1.py is something like this:

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.

How can I make py.test to inherit the above test
and run both after executing py.test test_file2.py ?

Something like this ( test_file2.py Contents):

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 :

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.

In other words, here is a really simple example with 3 files:

File 1: hello_world.py :

def hello_world():
    print('hello world!')

hello_world()

File 2: print_text.py :

def print_text():
    print('foo bar baz')

print_text()

File 3: run_everything.py :

import hello_world
import print_text

Result when you run 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 :

    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:

    import test_file1

    test_file1.test_connect()

First one create a fixture in 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()

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

PS

It possible to use any other modules, just inject they are into your tests with pytest_plugins directive:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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