1 # Python 2.7+ Dictionary Comprehension
2 map = {'a': 1, 'b': 2}
3 inv_map {v: k for k, v in map.items()}
4
5 # Python 2.7+ with statements
6 with open("out.txt","wt"), open("in.txt") as file_out, file_in:
7 pass
1 # Python 2.7+ contains more assertions
2 items1 = {'product': 'Worry-Free'}
3 items2 = {'product': 'OfficeScan'}
4 self.assertDictEqual(items1, items2)
1 language: python
2 python:
3 - 2.6
4 - 2.7
5 - 3.2
6 - 3.3
7 install:
8 - pip install --use-mirrors tox
9 script: TOXENV=py$(echo $TRAVIS_PYTHON_VERSION | tr -d .) tox -v
1 [tox]
2 envlist = py26,py27
3
4 [testenv]
5 deps=pytest
6 commands=py.test
1 from distutils.core import setup
2 from babel.messages import frontend as babel
3
4 setup(
5 ...
6 cmdclass={'compile_catalog': babel.compile_catalog,
7 'extract_messages': babel.extract_messages,
8 'init_catalog': babel.init_catalog,
9 'update_catalog': babel.update_catalog}
10 )
GLOB sdist-make: /Users/hubert/tmp/samplepy/setup.py
...
msg=packaging
cmdargs=['/Users/hubert/py-envs/py27env/bin/python',
local('/Users/hubert/tmp/samplepy/setup.py'), 'sdist',
'--formats=zip', '--dist-dir',
local('/Users/hubert/tmp/samplepy/.tox/dist')]
env=None
Traceback (most recent call last):
File "setup.py", line 2, in <module>
from babel.messages import frontend as babel
ImportError: No module named babel.messages
ImportError: No module named babel.messages
install_requires
dependency_links
1 from distutils.core import setup
2 setup(
3 ...
4 dependency_links=[
5 'http://github.com/celery/celery/tarball/master#egg=celery'
6 ]
7 )
1 from distutils.core import setup
2 setup(
3 ...
4 dependency_links=[
5 'git+https://example.com/spamneggs/foobar.git#egg=foobar-1.2.3'
6 ]
7 )
1 from distutils.core import setup
2 setup(
3 ...
4 dependency_links=[
5 'file:../../../deps/foobar#foobar'
6 ]
7 )
1 [tox]
2 envlist = py26, py27
3
4 [testenv]
5 commands = nosetests {posargs:--with-cov --cov-report=xml --with-xunit --cov package}
6 flake8 --exit-zero package
7
8 deps = nose
9 nose-cov
10 coverage
11 mock
12 flake8
13
14 [testenv:py26]
15 basepython={homedir}/.pythonbrew/pythons/Python-2.6.8/bin/python
16
17 [testenv:py27]
18 basepython={homedir}/.pythonbrew/pythons/Python-2.7.5/bin/python
import unittest
assertEqual
1 def test_simple_dict_compare_1(self):
2 dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
3 dict2 = {'a': 1, 'b': 1, 'c': 3, 'd': 5}
4
5 self.assertEqual(dict1, dict2)
AssertionError: {'a': 1, 'c': 3, 'b': 2, 'd': 4} !=
{'a': 1, 'c': 3, 'b': 1, 'd': 5}
import testfixtures
compare
compare
好讀多了1 def test_simple_dict_compare_2(self):
2 dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
3 dict2 = {'a': 1, 'b': 1, 'c': 3, 'd': 5}
4
5 compare(dict1, dict2)
AssertionError: dict not as expected:
same:
['a', 'c']
values differ:
'b': 2 != 1
'd': 4 != 5
compare
遇到 JSONsame:
[u'completed_in', u'max_id_str', u'next_page', u'page']]
values differ:
u'max_id': 122078461840982020 != 122078461840982021
u'results': [{u'created_at': u'Thu, 06 Oct 2011 19:36:17 +0000',
u'entities': {u'urls': [{u'display_url': u'bit.ly/q9fyz9',
u'expanded_url': u'http://bit.ly/q9fyz9',
u'indices': [37, 57],
u'url': u'http://t.co/L9JXJ2ee'}]},
...
以下三千行
u'geo': None,
- u'id': 122033350327279620,
? ^
+ u'id': 122033350327279621,
?
pip install unittest2
compare
還不夠好,但是也還堪用Replacer
1 @patch('hello.yoyo.ClassB')
2 @patch('hello.yoyo.ClassA')
3 def test_hello(self, MockClassA, MockClassB):
4 pass
1 def test_mock(self):
2 # python 2.6, or using contextlib
3 with patch('test_hello.RealClassA') as mock_a:
4 with patch('test_hello.RealClassB') as mock_b:
5 pass
6
7 def test_mock_27(self):
8 # python 2.7+
9 with patch('test_hello.RealClassA') as mock_a, \
10 with patch('test_hello.RealClassB') as mock_b:
11
12 pass
Replacer
1 with Replacer() as r:
2 r.replace('test_hello.RealClassA', MagicMock())
3 r.replace('test_hello.RealClassB', MagicMock())
4
5 instance_a = RealClassA()
6 instance_b = RealClassB()
patch
1 def setUp(self):
2 self.patcher_a = patch('test_hello.RealClassA', spec=True)
3 self.patcher_b = patch('test_hello.RealClassB', spec=True)
4
5 self.mock_a = self.patcher_a.start()
6 self.mock_b = self.patcher_b.start()
7
8 def tearDown(self):
9 self.patcher_a.stop()
10 self.patcher_b.stop()
tearDown
會不會漏?Replacer
1 def setUp(self):
2 self.replacer = Replacer()
3
4 self.mock_a = MagicMock()
5 self.mock_b = MagicMock()
6
7 self.replacer.replace('test_hello.RealClassA', self.mock_a)
8 self.replacer.replace('test_hello.RealClassB', self.mock_b)
9
10 def tearDown(self):
11 self.replacer.restore()
testfixtures
其他的好東西TempDirectory
超好用ShouldRaises
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |