Guest User

Untitled

a guest
Nov 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import inspect
  2. import unittest
  3.  
  4.  
  5. def is_called_by_nose():
  6. stack = inspect.stack()
  7. return any(x[0].f_globals['__name__'].startswith('nose.') for x in stack)
  8.  
  9.  
  10. class TestFoo(unittest.TestCase):
  11. def test_foo(self):
  12. self.assertTrue(is_called_by_nose())
  13.  
  14. $ python -m nose test_caller
  15. .
  16. ----------------------------------------------------------------------
  17. Ran 1 test in 0.009s
  18.  
  19. OK
  20. $ nosetests test_caller
  21. .
  22. ----------------------------------------------------------------------
  23. Ran 1 test in 0.009s
  24.  
  25. OK
  26. $ python -m unittest test_caller
  27. F
  28. ======================================================================
  29. FAIL: test_foo (test_caller.TestFoo)
  30. ----------------------------------------------------------------------
  31. Traceback (most recent call last):
  32. File "test_caller.py", line 14, in test_foo
  33. self.assertTrue(is_called_by_nose())
  34. AssertionError: False is not true
  35.  
  36. ----------------------------------------------------------------------
  37. Ran 1 test in 0.004s
  38.  
  39. FAILED (failures=1)
Add Comment
Please, Sign In to add comment