python - 仅对 python 中的 root 用户进行单元测试

标签 python

python 的单元测试库(尤其是 3.x,我不太关心 2.x)是否有装饰器只能由 root 用户访问?

我有这个测试功能。

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

blabla 函数只能由 root 执行。我只想要 root 用户装饰器,所以普通用户将跳过此测试:

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

这样的装饰器存在吗?不过,我们有 @support.cpython_only 装饰器。

最佳答案

如果您使用的是单元测试,则可以使用 unittest.skipIfunittest.skipUnless 跳过测试或整个测试用例。

在这里,你可以做:

import os

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

可以简化为(可读性较差):

@unittest.skipIf(os.getuid())

关于python - 仅对 python 中的 root 用户进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18098110/

相关文章:

python - 谷歌地球引擎 : Time series of reduceRegion()

python - 测试点是否在椭圆内,而不使用 Matplotlib?

python - 将聚合函数应用于数据表列并返回值,而不是数据表

python - 如何解决 Matplotlib 类型错误 : only integer scalar arrays can be converted to a scalar index

python - 如何从可以在 VBA 中调用的简单 python 代码创建 dll

Python 正则表达式 : split vsv column

python - 如何从 Bokeh 中的 DataTable 对象中选择文本

python - Django +Celery +SQS -> boto.exception.SQSError : SQSError: 599 gnutls_handshake()

Python:如何对自定义 HTTP 请求处理程序进行单元测试?

python - 在python中快速读取HDF 5文件?