Unit test only for root user in python

Does the library use unit test for python (especially 3.x, I really don't like 2.x), does the decorator have root access only?

I have this testing feature.

def test_blabla_as_root(): self.assertEqual(blabla(), 1) 

The blabla function can only be performed by root. I want the root user only for clearance, so a regular user will skip this test:

 @support.root_only def test_blabla_as_root(): self.assertEqual(blabla(), 1) 

Is there such a decorator? We have @ support.cpython_only decorator, though.

+7
python
source share
1 answer

If you use unittest, you can skip tests or entire test cases using unittest.skipIf and unittest.skipUnless .

Here you can do:

 import os @unittest.skipUnless(os.getuid() == 0) # Root has an uid of 0 def test_bla_as_root(self): ... 

What can be simplified in (less readable):

 @unittest.skipIf(os.getuid()) 
+6
source share

All Articles