Does unit test library for python (especially 3.x, I don't really care about 2.x) has decorator to be accessed only by root user?
I have this testing function.
def test_blabla_as_root():
self.assertEqual(blabla(), 1)
blabla function can only be executed by root. I want root user only decorator so normal user will skip this test:
@support.root_only
def test_blabla_as_root():
self.assertEqual(blabla(), 1)
Does such decorator exist? We have @support.cpython_only decorator though.
If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.
Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.
Yes. unittest is a xUnit style frameworkfor Python, it was previously called PyUnit.
If you're using unittest, you can skip tests or entire test cases using unittest.skipIf
and unittest.skipUnless
.
Here, you could do:
import os
@unittest.skipUnless(os.getuid() == 0) # Root has an uid of 0
def test_bla_as_root(self):
...
Which could be simplified in a (less readable):
@unittest.skipIf(os.getuid())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With