Harry Percival @hjwp www.obeythetestinggoat.com www.pythonanywhere.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def absolute(fn): | |
| # this decorator currently does nothing | |
| def modified_fn(x): | |
| return fn(x) | |
| return modified_fn | |
| def foo(x): | |
| return 1 - x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="text/javascript"> | |
| $(function() { | |
| // normal application code goes here | |
| // eg initialize Ace text editor | |
| MyApp.do.myStuff("etc"); | |
| // ... and then "reinitialize" togetherjs so that it finds all our dynamic ui stuff | |
| TogetherJS.reinitialize(); | |
| }); | |
| </script> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get_ipython().system(u'sqlite3 my.db') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import subprocess | |
| script = '/home/harry/.virtualenvs/my-project-name/bin/postactivate' # eg | |
| env_to_json = 'import os, json; print(json.dumps(dict(os.environ)))' | |
| environ_json = subprocess.check_output( | |
| '. {} && python -c "{}"'.format(script, env_to_json), | |
| shell=True, env={} | |
| ) | |
| os.environ.update(json.loads(environ_json.decode('utf8'))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tempfile | |
| from selenium import webdriver | |
| htmlfile = tempfile.NamedTemporaryFile(suffix='.html', delete=False) | |
| with htmlfile.file: | |
| htmlfile.file.write('<!DOCTYPE html><html><input type="text" autocapitalize="off" /></html>') | |
| def check_browser(browser): | |
| browser.implicitly_wait(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pytest | |
| @pytest.yield_fixture | |
| def print_on_fail(): | |
| yield | |
| print('test failed') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import pytest | |
| def pytest_runtest_call(item): | |
| if hasattr(item, "_request"): | |
| if hasattr(item._request, "_addoutput_on_failure"): | |
| item._request._addoutput_on_failure() | |
| @pytest.fixture |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ➜ t2 py.test tests.py | |
| ============================= test session starts ============================= | |
| platform linux -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2 | |
| rootdir: /tmp/t2, inifile: | |
| collected 2 items | |
| tests.py .F | |
| ================================== FAILURES =================================== | |
| ______________________________ test_should_fail _______________________________ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pytest | |
| @pytest.mark.hookwrapper | |
| def pytest_pyfunc_call(pyfuncitem): | |
| yield | |
| print('pyfunc call after') | |
| if 'watch_logs' in pyfuncitem.funcargs: | |
| print(pyfuncitem.funcargs['watch_logs']()) | |