Gentoo Archives: gentoo-python

From: Arfrever Frehtes Taifersar Arahesis <arfrever.fta@×××××.com>
To: gentoo-python@l.g.o
Subject: Re: [gentoo-python] recent trend of django-foo tests
Date: Mon, 03 Jun 2013 07:05:44
Message-Id: 201306030905.07511.Arfrever.FTA@gmail.com
In Reply to: Re: [gentoo-python] recent trend of django-foo tests by Mike Gilbert
1 2013-05-30 21:34 Mike Gilbert napisał(a):
2 > python -c "code" foo.py does NOT run foo.py.
3
4 $ cat my_script
5 #!/usr/bin/python
6
7 import sys
8 import unittest
9
10 class SomeTests(unittest.TestCase):
11 def test_something(self):
12 self.assertTrue(sys.something > 0)
13
14 if __name__ == "__main__":
15 unittest.main(verbosity=2)
16 $ diff -u my_script my_module.py
17 $ python my_script
18 test_something (__main__.SomeTests) ... ERROR
19
20 ======================================================================
21 ERROR: test_something (__main__.SomeTests)
22 ----------------------------------------------------------------------
23 Traceback (most recent call last):
24 File "my_script", line 8, in test_something
25 self.assertTrue(sys.something > 0)
26 AttributeError: 'module' object has no attribute 'something'
27
28 ----------------------------------------------------------------------
29 Ran 1 test in 0.001s
30
31 FAILED (errors=1)
32 $ python -m my_module
33 test_something (__main__.SomeTests) ... ERROR
34
35 ======================================================================
36 ERROR: test_something (__main__.SomeTests)
37 ----------------------------------------------------------------------
38 Traceback (most recent call last):
39 File "./my_module.py", line 8, in test_something
40 self.assertTrue(sys.something > 0)
41 AttributeError: 'module' object has no attribute 'something'
42
43 ----------------------------------------------------------------------
44 Ran 1 test in 0.001s
45
46 FAILED (errors=1)
47 $ python -c 'import runpy, sys; sys.something=1; runpy.run_path("my_script", run_name="__main__")'
48 test_something (__main__.SomeTests) ... ok
49
50 ----------------------------------------------------------------------
51 Ran 1 test in 0.000s
52
53 OK
54 $ python -c 'import runpy, sys; sys.something=1; runpy.run_module("my_module", run_name="__main__", alter_sys=True)'
55 test_something (__main__.SomeTests) ... ok
56
57 ----------------------------------------------------------------------
58 Ran 1 test in 0.000s
59
60 OK
61 $
62
63 :)
64
65 --
66 Arfrever Frehtes Taifersar Arahesis

Attachments

File name MIME type
signature.asc application/pgp-signature