課é¡ã¨è³ªå ========== .. Some Issues and Questions ================================== .. If you don't find an answer here, checkout the :ref:`contact channels` to get help. .. note:: ããã§çããè¦ã¤ãããªãå ´å㯠:ref:`contact channels` ããå©ããæ±ãã¦ãã ããã .. On naming, nosetests, licensing and magic ------------------------------------------------ ååä»ããnosetestsãã©ã¤ã»ã³ã¹ã¨éæ³ ------------------------------------- .. Why a ``py.test`` instead of a ``pytest`` command? ++++++++++++++++++++++++++++++++++++++++++++++++++ ã©ããã¦ã³ãã³ãå㯠``pytest`` ã§ã¯ãªã ``py.test`` ãªã®ï¼ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. Some of the reasons are historic, others are practical. ``py.test`` used to be part of the ``py`` package which provided several developer utilities, all starting with ``py.<TAB>``, thus providing nice TAB-completion. If you install ``pip install pycmd`` you get these tools from a separate package. These days the command line tool could be called ``pytest`` but since many people have gotten used to the old name and there is another tool named "pytest" we just decided to stick with ``py.test``. çç±ã®ä¸é¨ã¯æ´å²çãªãã®ã§ããã以å¤ã¯å®ç¨ä¸ã®ãã®ã§ãã ``py.test`` ã¯ãè¤æ°ã®éçºè åãã¦ã¼ãã£ãªãã£ãæä¾ãã ``py`` ããã±ã¼ã¸ã®ä¸é¨ã¨ãã¦ä½¿ããã¦ãã¾ãããããã¯å ¨ã¦ ``py.<TAB>`` ã§å§ã¾ãããã®ããã« <TAB> ãè£å®ããåªããæ©è½ãæä¾ãã¦ãã¾ãã ``pip install pycmd`` ã§ã¤ã³ã¹ãã¼ã«ããããå¥ã ã®ããã±ã¼ã¸ãããããã£ããã¼ã«ã確èªã§ãã¾ããæè¿ã«ãªã£ã¦ãã³ãã³ãã©ã¤ã³ãã¼ã«ã¯ ``pytest`` ã¨å¼ãã§ãã¾ãããæããã®å¤ãã®äººãã¡ãå¤ãååã«ãªããã§ã㦠"pytest" ã¨ããååã¯å¥ãã¼ã«ã«æãã¾ãããã®ãããæã 㯠``py.test`` ã¨ããååã使ãç¶ãããã¨ã«æ±ºãã¾ããã .. How does py.test relate to nose and unittest? +++++++++++++++++++++++++++++++++++++++++++++++++ py.test 㯠nose ã unittest ã¨ã©ããªé¢ä¿ãããã®ï¼ ++++++++++++++++++++++++++++++++++++++++++++++++++ .. py.test and nose_ share basic philosophy when it comes to running and writing Python tests. In fact, you can run many tests written for nose with py.test. nose_ was originally created as a clone of ``py.test`` when py.test was in the ``0.8`` release cycle. Note that starting with pytest-2.0 support for running unittest test suites is majorly improved and you should be able to run many Django and Twisted test suites without modification. py.test 㨠nose_ ã¯ãPython ãã¹ããæ¸ãã¦å®è¡ããã®ã«åãåºæ¬ç念ããã£ã¦ãã¾ãã nose_ ã¯ããã¨ã㨠``py.test`` ã ``0.8`` ãªãªã¼ã¹ã®ã¨ãã« py.test ã®ã¯ãã¼ã³ã¨ãã¦ä½æããã¾ãããpytest 2.0 㯠unittest ã®ãã¹ãã¹ã¤ã¼ããå®è¡ã§ããããã«ãªã£ãã®ã主ãªæ¹åç¹ã§ãããã¨ã«æ³¨ç®ãã¦ãã ãããããã¦ãå¤ãã® Django ã Twisted ã®ãã¹ãã¹ã¤ã¼ããå¤æ´ããã«å®è¡ã§ãã¾ãã .. _features: test/features.html py.test ã® "éæ³" ã¯ä¸ä½ä½ãªã®ï¼ ++++++++++++++++++++++++++++++++ .. What's this "magic" with py.test? ++++++++++++++++++++++++++++++++++++++++++ .. Around 2007 (version ``0.8``) some people claimed that py.test was using too much "magic". Partly this has been fixed by removing unused, deprecated or complicated code. It is today probably one of the smallest, most universally runnable and most customizable testing frameworks for Python. However, ``py.test`` still uses many metaprogramming techniques and reading its source is thus likely not something for Python beginners. 2007å¹´é (ãã¼ã¸ã§ã³ ``0.8``)ãpy.test ã¯ãã¾ãã«ãå¤ãã® "éæ³" ã使ã£ã¦ããã¨ä¸»å¼µãã人ãã¡ããã¾ãããæªä½¿ç¨ãªã³ã¼ããéæ¨å¥¨ãè¤éãªã³ã¼ããåé¤ãããã¨ã§é¨åçã«ã¯è§£æ¶ããã¾ãããä»æ¥ã§ã¯ãpy.test ã¯ç¢ºãã« Python åãã®æãå°ããæ®éçã§ã«ã¹ã¿ãã¤ãºå¯è½ãªãã¹ããã¬ã¼ã ã¯ã¼ã¯ã®1ã¤ã§ããä½ã ``py.test`` ã¯ãã¾ã å¤ãã®ã¡ã¿ããã°ã©ãã³ã°ãã¯ããã¯ã使ã£ã¦ãã¦ãPython åå¿è ããã®ã½ã¼ã¹ãèªãããã®ã§ã¯ããã¾ããã .. A second "magic" issue is arguably the assert statement debugging feature. When loading test modules py.test rewrites the source code of assert statements. When a rewritten assert statement fails, its error message has more information than the original. py.test also has a second assert debugging technique. When an ``assert`` statement that was missed by the rewriter fails, py.test re-interprets the expression to show intermediate values if a test fails. This second technique suffers from a caveat that the rewriting does not: If your expression has side effects (better to avoid them anyway!) the intermediate values may not be the same, confusing the reinterpreter and obfuscating the initial error (this is also explained at the command line if it happens). You can turn off all assertion debugging with ``py.test --assertmode=off``. 2çªç®ã® "éæ³" ã®èª²é¡ã¯ãééããªã assert æã®ãããã°æ©è½ã§ãããã¹ãã¢ã¸ã¥ã¼ã«ãèªã¿è¾¼ã¾ããã¨ãpy.test 㯠assert æã®ã½ã¼ã¹ã³ã¼ããæ¸ãæãã¾ããæ¸ãæãããã assert æã失æããã¨ãããã®ã¨ã©ã¼ã¡ãã»ã¼ã¸ã¯ããªãªã¸ãã«ã® assert æããåããããããã®ã§ããpy.test ã«ãå¥ã®ãããã°ææ³ãããã¾ããæ¸ãæãã失æãããã¨ã«ãã ``assert`` æã失æããã¨ããpy.test ã¯ãã¹ãã失æããã¨ãã«ä¸éå¤ã表示ããããã«ãã®å¼ãå解éãã¾ãããã®å¥ã®ãããã°ææ³ã¯æ¸ãæããè¡ãããªãã£ãã¨ããè¦åã§æ©ã¾ããã¾ãããã®å¼ãå¯ä½ç¨ (ã¨ã«ãã触ããªãã®ãè¯ãï¼) ããã¤ãªããä¸éå¤ã¯åãã«ãªããªãå¯è½æ§ãããã¾ããããã¯å解éããã¤ã³ã¿ã¼ããªã¿ã¼ãæ··ä¹±ãããåæã®ã¨ã©ã¼ãåããé£ããã¾ã (ãããçºçãããã³ãã³ãã©ã¤ã³ã§è¡¨ç¤ºããã) ã ``py.test --assertmode=off`` ã«ãããå ¨ã¦ã®ã¢ãµã¼ã·ã§ã³ãããã°ãç¡å¹ã«ã§ãã¾ãã .. _`py namespaces`: index.html .. _`py/__init__.py`: http://bitbucket.org/hpk42/py-trunk/src/trunk/py/__init__.py é¢æ°ã®å¼æ°ããã©ã¡ã¼ã¿ã¼ãã¹ãã¨ã»ããã¢ãã -------------------------------------------- .. Function arguments, parametrized tests and setup ------------------------------------------------------- .. _funcargs: test/funcargs.html funcarg- 対 xUnit ã»ããã¢ããã¹ã¿ã¤ã«ã®çå ++++++++++++++++++++++++++++++++++++++++++++ .. Is using funcarg- versus xUnit setup a style question? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. For simple applications and for people experienced with nose_ or unittest-style test setup using `xUnit style setup`_ probably feels natural. For larger test suites, parametrized testing or setup of complex test resources using funcargs_ may feel more natural. Moreover, funcargs are ideal for writing advanced test support code (like e.g. the monkeypatch_, the tmpdir_ or capture_ funcargs) because the support code can register setup/teardown functions in a managed class/module/function scope. ã·ã³ãã«ãªã¢ããªã±ã¼ã·ã§ã³åãããnose_ ã unittest ã¹ã¿ã¤ã«ã®çµé¨ããã人ãã¡ã«ã¨ã£ã¦ã¯ããããã :ref:`xunitsetup` ã使ãæ¹ãèªç¶ã«æããã¯ãã§ãããããã巨大ãªãã¹ãã¹ã¤ã¼ãåãã§ã¯ããã©ã¡ã¼ã¿ã¼ãã¹ãã funcargs_ ã使ã£ãè¤éãªãã¹ããªã½ã¼ã¹ã®ã»ããã¢ããã®æ¹ããã£ã¨èªç¶ã«æããããããã¾ãããããã«è¨ãã¨ãfuncargs ã¯é«åº¦ãªãã¹ããµãã¼ãã³ã¼ã (ä¾ãã° monkeypatch_, tmpdir_, capture_, funcargs) ãæ¸ãã®ã«æé©ã§ããã¨ããã®ã¯ããã®ãµãã¼ãã³ã¼ã㯠class/module/function ã¹ã³ã¼ãã管çãã setup/teardown é¢æ°ãç»é²ã§ããããã§ãã .. _monkeypatch: test/plugin/monkeypatch.html .. _tmpdir: test/plugin/tmpdir.html .. _capture: test/plugin/capture.html .. _`why pytest_pyfuncarg__ methods?`: ã©ãã㦠funcarg ãã¡ã¯ããªã¼ã®åå㯠``pytest_funcarg__*`` ãªã®ï¼ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. Why the ``pytest_funcarg__*`` name for funcarg factories? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. We like `Convention over Configuration`_ and didn't see much point in allowing a more flexible or abstract mechanism. Moreover, it is nice to be able to search for ``pytest_funcarg__MYARG`` in source code and safely find all factory functions for the ``MYARG`` function argument. æã 㯠`è¨å®ããè¦ç´`_ ã好ã¿ãããæè»ã«æ½è±¡çãªä»çµã¿ã許容ããã®ã«æå³ãããã¨ã¯æãã¾ããã§ãããããã«ãã½ã¼ã¹ã³ã¼ãå 㧠``pytest_funcarg__MYARG`` ãæ¤ç´¢ã§ããã®ã¯ä¾¿å©ã§ã ``MYARG`` ã¨ããé¢æ°ã®å¼æ°ã«å¯¾ããå ¨ã¦ã®ãã¡ã¯ããªã¼é¢æ°ãæ¸æããªãæ¢ãã¾ãã .. _`Convention over Configuration`: http://en.wikipedia.org/wiki/Convention_over_Configuration .. _`è¨å®ããè¦ç´`: http://en.wikipedia.org/wiki/Convention_over_Configuration funcarg ãã¡ã¯ããªã¼é¢æ°ããè¤æ°ã®å¤ã yield ã§ãã¾ããï¼ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. Can I yield multiple values from a fixture function function? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. There are two conceptual reasons why yielding from a factory function is not possible: ãã¡ã¯ããªã¼é¢æ°ã yield ã§ããªãæ¦å¿µä¸ã®çç±ã2ã¤ããã¾ã: .. * Calling factories for obtaining test function arguments is part of setting up and running a test. At that point it is not possible to add new test calls to the test collection anymore. * ãã¹ãé¢æ°ã®å¼æ°ãåå¾ããããã«ãã¡ã¯ããªã¼é¢æ°ãå¼ã³åºãã®ã¯ããã¹ãã®è¨å®ã¨å®è¡ã®é¨åã§ãããã®æç¹ã§ã¯ããã¹ãã³ã¬ã¯ã·ã§ã³ã®ããã«æ°ããªãã¹ãå¼ã³åºãã追å ã§ãã¾ããã .. * If multiple factories yielded values there would be no natural place to determine the combination policy - in real-world examples some combinations often should not run. * è¤æ°ã®ãã¡ã¯ããªã¼é¢æ°ãå¤ã yield ããå ´åãçµã¿åããæ¹æ³ã決å®ããã®ã«é©å½ãªå ´æãããã¾ãããç¾å®ã®ä¸çã®ä¾ã¯ããããã£ãçµã¿åãããå¤ãã®å ´åã«å®è¡ããã¾ããã .. Use the `pytest_generate_tests`_ hook to solve both issues and implement the `parametrization scheme of your choice`_. 両æ¹ã®èª²é¡ã解決ããããã« `pytest_generate_tests`_ ããã¯ã使ãã `ãã©ã¡ã¼ã¿ã¼åã®ä»çµã¿ã«ãã£ããã®ãé¸æãã¦`_ å®è£ ãã¦ãã ããã .. _`pytest_generate_tests`: test/funcargs.html#parametrizing-tests .. _`parametrization scheme of your choice`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/ .. _`ãã©ã¡ã¼ã¿ã¼åã®ä»çµã¿ã«ãã£ããã®ãé¸æãã¦`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/ ãã®ä»ã®ããã±ã¼ã¸ã¨ py.test ã®ç¸äºé£æº --------------------------------------- .. py.test interaction with other packages --------------------------------------------------- .. Issues with py.test, multiprocess and setuptools? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ py.test, multiprocess, setuptools ã¨é¢é£ããåé¡ï¼ ++++++++++++++++++++++++++++++++++++++++++++++++++ .. On windows the multiprocess package will instantiate sub processes by pickling and thus implicitly re-import a lot of local modules. Unfortunately, setuptools-0.6.11 does not ``if __name__=='__main__'`` protect its generated command line script. This leads to infinite recursion when running a test that instantiates Processes. Windows ä¸ã® multiprocess ããã±ã¼ã¸ã¯ãpickle åãããã¨ã§ãµãããã»ã¹ãã¤ã³ã¹ã¿ã³ã¹åããæé»çã«ããããã®ãã¼ã«ã«ã¢ã¸ã¥ã¼ã«ãåã¤ã³ãã¼ããã¾ããæ®å¿µãªãããsetuptools 0.6.11 ãä½æããã³ãã³ãã©ã¤ã³ã¹ã¯ãªãã㯠``if __name__=='__main__'`` ã«ããä¿è·ãããã¾ãããããã«ãããå®è¡ä¸ã®ãã¹ããããã»ã¹ãã¤ã³ã¹ã¿ã³ã¹åããã¨ãã«ç¡éå帰ãå¼ãèµ·ããã¾ãã .. A good solution is to `install Distribute`_ as a drop-in replacement for setuptools and then re-install ``pytest``. Otherwise you could fix the script that is created by setuptools by inserting an ``if __name__ == '__main__'``. Or you can create a "pytest.py" script with this content and invoke that with the python version:: è¯ã解決çã¯ãsetuptools ã®ç½®ãæãã¨ã㦠`distribute ãã¤ã³ã¹ãã¼ã«ãã`_ ãã¨ã§ãããã®å¾ã« ``pytest`` ãåã¤ã³ã¹ãã¼ã«ãã¾ããå¥ã®æ¹æ³ã§ã¯ãsetuptools ãä½æããã¹ã¯ãªããã« ``if __name__ == '__main__'`` ã追å ãã¦ä¿®æ£ãã¾ãããããã¯ããã®å 容ãå«ã "pytest.py" ã¹ã¯ãªãããä½æãã¦ããã®ã¹ã¯ãªãããå®è¡ãã¾ã:: import pytest if __name__ == '__main__': pytest.main() .. _`install distribute`: http://pypi.python.org/pypi/distribute#installation-instructions .. _`distribute ãã¤ã³ã¹ãã¼ã«ãã`: http://pypi.python.org/pypi/distribute#installation-instructions .. include:: links.inc