Python:基于nosetest是否正在运行的条件变量

时间:2011-09-07 21:57:44

标签: python nose peewee

我正在运行nosetests,它具有需要加载与生产数据库不同的数据库的设置功能。我正在使用的ORM是peewee,它要求在定义中设置模型的数据库。

所以我需要设置一个条件变量,但我不知道使用什么条件来检查nosetest是否正在运行该文件。

我在Stack Overflow上读到你可以检查nose中的sys.modules,但我想知道是否有更准确的方法来检查鼻子是否正在运行。

2 个答案:

答案 0 :(得分:10)

或许检查sys.argv[0]以查看正在运行的命令?

答案 1 :(得分:0)

检查sys.argv可能有效,但你可以使用 public static int GenerateCode(SecurityToken securityToken, TimeSpan timeStep ,string modifier = null) { if (securityToken == null) { throw new ArgumentNullException("securityToken"); } _timestep = timeStep; // Allow a variance of no greater than 90 seconds in either direction var currentTimeStep = GetCurrentTimeStepNumber(); using (var hashAlgorithm = new HMACSHA1(securityToken.GetDataNoClone())) { return ComputeTotp(hashAlgorithm, currentTimeStep, modifier); } } nosetests执行鼻子,这显然会给你一个不同的结果。

我认为更强大的方法是检查堆栈并查看是否通过名为python -m nose的包调用代码。

示例代码:

nose

使用示例:

import inspect
import unittest


def is_called_by_nose():
    stack = inspect.stack()
    return any(x[0].f_globals['__name__'].startswith('nose.') for x in stack)


class TestFoo(unittest.TestCase):
    def test_foo(self):
        self.assertTrue(is_called_by_nose())