जांच sys.argv काम कर सकता है, लेकिन आप 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())
उदाहरण उपयोग:
$ python -m nose test_caller
.
----------------------------------------------------------------------
Ran 1 test in 0.009s
OK
$ nosetests test_caller
.
----------------------------------------------------------------------
Ran 1 test in 0.009s
OK
$ python -m unittest test_caller
F
======================================================================
FAIL: test_foo (test_caller.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_caller.py", line 14, in test_foo
self.assertTrue(is_called_by_nose())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 1 test in 0.004s
FAILED (failures=1)
'आयात sys; परीक्षण = sys.argv [0] .endswith ('nosetests') ' – msiemens