简体   繁体   中英

Executing multiple pytest test files by using one “master” file in Python

I have multiple test files in the test folder. My directory structure is like:

Tests/
  run-tests.py
  pytest.ini
  /TestCases
    TestCase1.py
    TestCase2.py 

My run-tests.py file contains:

from __future__ import absolute_import    
import pytest

if __name__ == '__main__':
    pytest.main(args=['TestsCases'])

So when I run run-tests.py from console:

python3.8 -m pytest Tests/run-tests.py

tests are not executed. Of course I could create shell script and call them inside the script by explicitly calling them but this is what I do not want to do. TestCase1.py and other file contains tests which use pytest framework and are defined as functions. I am not using classes inside.

So question: is it possible to execute TestCase*.py files by using run-test.py file?

Also I wonder how the pytest.xml files will be generated. My pytest.ini file contains:

addopts = -v -s -ra --junitxml=Tests/test-reports/pytest.xml

Would be very nice to be able to merge all TestCase*.py exports into single one report xml file. I am afraid that every TestCase*.py file will overwrite the previous one.

I found the solution. Inside my run-tests.py file I have a loop from which I call every test:

test_cases_folder = os.path.dirname(os.path.realpath(__file__)) + '/TestCases/'
for name in os.listdir(test_cases_folder):
    if fnmatch.fnmatch(name, 'Test*.py'):
        pytest.main(
            [test_cases_folder+'TestCase'+str(i)+'.py', '--url='+args.server, '--user='+args.user, '--headless='+args.headless]
        )

also I am executing script as a module, not by calling file directly:

python3.8 -m Tests.run-tests --server=<hostname> --user=<username> --headless=False|True

hopefully it will help somebody

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