AliyevH
(Hasan)
September 6, 2021, 5:49pm
1
I would like to propose to add new feature to unittest, that can check existence of file or folder.
What are your advices except workarounds?
import unittest
class TestSomething(unittest.TestCase):
def test_file_is_installed(self):
self.assertIsFile('path_to_file')
self.assertisFolder('path_to_folder')
cameron
(Cameron Simpson)
September 6, 2021, 10:48pm
2
There are so many of these, and all are very special purpose. There are
some modules in PyPI providing additional assert* methods, IIRC.
A common approach is to make a mixin class like this:
from os.path import isdir, isfile
class PathTestsMixin:
def assertIsFolder(self, path, msg="not a folder"):
return self.assertTrue(isdir(path), msg)
def assertIsFile(self, path, msg="not a file"):
return self.assertTrue(isfile(path), msg)
and to define your test class like this:
class TestSomething(unittest.TestCase, PathTestsMixin):
and it will have those methods available. By putting the mixin in a
utility module you can then use it in many test files.
Cheers,
Cameron Simpson cs@cskk.id.au
AliyevH
(Hasan)
September 7, 2021, 10:00am
3
@cameron thanks for reply. For now i am using Mixins for custom asserts. i thought maybe it could be useful to make this features available in unittest.