Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest: no tests ran

I have the following class file and a corresponding test file

dir.py:

import os


class Dir:
    def __init__(self, path=''):
        self.path = path

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, path):
        abspath = os.path.abspath(path)
        if abspath.exists():
            self._path = path
        else:
            raise IOError(f'{path} does not exist')

and dir_test.py:

import unittest

from ..dir import Dir


class TestDir(unittest.TestCase):

    def IOErrorIfPathNotExists(self):
        with self.assertRaises(IOError):
            Dir.path = "~/invalidpath/"
        with self.assertRaises(IOError):
            Dir('~/invalidpath/')


if __name__ == "__main__":
    unittest.main()

but when I run

pytest -x dir_test.py

it just prints no tests ran in 0.01 seconds

and I have no idea why. It is my first time using pytest except with exercises from exercism.io, and I can't spot any difference to their test files.

I am running it in a virtual environment (Python 3.6.5), with pytest and pytest-cache installed via pip.

like image 281
Takayama Avatar asked May 12 '26 04:05

Takayama


1 Answers

That's because your test method is not named properly.

By default, pytest will consider any class prefixed with Test as a test collection.

Yours is TestDir, this matches.

By default, pytest will consider any function prefixed with test as a test.

Yours is IOErrorIfPathNotExists, which does not start with test and is not executed.

Source.

like image 89
hoefling Avatar answered May 14 '26 19:05

hoefling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!