Sahand Jalali
Sahand Jalali

Reputation: 15

How can I import some functions and classes from modules into test file?

How can I import NestedValueError class & validate_item function from modules into test.py ?

Should I use conftest.py as a mediator between modules and tests file or something?

# /modules/errors.py 

class EmptyValueError(Exception):
      pass 

#/modules/validate_item.py
def validate_item():
    pass

#/tests/test.py

import pytest 

def test_item_validate_exception_nested_value():
    with pytest.raises(EmptyValueError):
        validate_item({})    

Upvotes: 0

Views: 53

Answers (1)

iBug
iBug

Reputation: 37227

Simple:

from modules.errors import EmptyValueError 
from modules.validate_item import validate_item

But be sure to run pytest in /, with the following command:

python3 -m pytest test/

From pytest documentation (linked above):

This is almost equivalent to invoking the command line script pytest [...] directly, except that calling via python will also add the current directory to sys.path.

Upvotes: 1

Related Questions