nanunga
nanunga

Reputation: 213

Running a pytest test from a python file and NOT from a command line

I have three python files in one directory (c:\Tests), I am trying to run the test using pytest from the file TestCases1.py but I have not succeed. I am new to python and I do not know if I am asking the right question. I have seen several examples but almost all use the command line to run the test and I want to run them from a python file. Since I am newbie to testing, I would appreciate a very simple answer (I have seen some similar questions but I did not get the answers). I am using Python 36-32 and Eclipse Oxygen 3a.

min_max.py => Some basic functions to be tested

def min(values):
    _min = values[0]
    for val in values:
        if val < _min:
            _min = val
    return _min

    

def max(values):
    _max = values[0]
    for val in values:
        if val > _max:
            _max = valal
    return _max

min_max_test.py => Some tests for the functions

import min_max

def test_min():
    print("starting")
    values = (2, 3, 1, 4, 6)
    val = min(values)
    assert val == 1
    print("done test_min")


def test_max():
    print("starting")
    values = (2, 3, 1, 4, 6)
    val = max(values)
    assert val == 6
    print("done test_max") 

TestCases1.py => File from where I want to run the test

import pytest
pytest_args = [
    'c:\Tests\min_max_test.py'
]

pytest.main(pytest_args)

Upvotes: 9

Views: 3944

Answers (3)

alv2017
alv2017

Reputation: 860

I would recommend to follow Pytest documentation: the pytest documentation explaining how to run tests from the Python code

Example:

  1. Let's assume that all the tests are stored in a directory called tests.

  2. In the project directory create a file run_tests_locally.py with the following content:

import sys
import pytest


if __name__ == "__main__":
    sys.exit(pytest.main(["-svv", "tests"]))
  1. Now you can run your tests from this particular Python script, you can also run your tests by clicking the Run button in your IDE. And most importantly now you can run your tests in debug mode. :)

Upvotes: 0

Miguel Trejo
Miguel Trejo

Reputation: 6687

Optionally, you could use subprocess to run pytest commands on your python script. For example,

# ~/tests
import subprocess
subprocess.run(["pytest . -q"], shell=True)
>>>
.                                                                                             [100%]
1 passed in 0.00s
CompletedProcess(args=['pytest . -q'], returncode=0)

Upvotes: 2

nikochiko
nikochiko

Reputation: 193

In min_max_test.py, the min and max variable names in the test functions would be taken from the built-ins and not from your min_max.py file.

You either need to use something like min_max.min or import those functions using a from import rather than a full module import.

P.S. please include the error messages along with the questions and be specific as to what problem you are having. makes it that much easier :)

Upvotes: 0

Related Questions