Unit test for only root user in python
Question: 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.


Answer: 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())
    

