[py-svn] commit/pytest: hpk42: added an example on how to do python2/python3 customized test collection

Bitbucket commits-noreply at bitbucket.org
Thu Jun 7 12:40:02 CEST 2012


1 new commit in pytest:


https://bitbucket.org/hpk42/pytest/changeset/2815a3d12bd0/
changeset:   2815a3d12bd0
user:        hpk42
date:        2012-06-07 12:39:53
summary:     added an example on how to do python2/python3 customized test collection
affected #:  1 file

diff -r 928db89125925202e651dba1c90032350f44773f -r 2815a3d12bd049d952e213b0748fd7ff849e605b doc/en/example/pythoncollection.txt
--- a/doc/en/example/pythoncollection.txt
+++ b/doc/en/example/pythoncollection.txt
@@ -43,7 +43,7 @@
 
     $ py.test --collectonly
     =========================== test session starts ============================
-    platform linux2 -- Python 2.7.1 -- pytest-2.2.4
+    platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev1
     collecting ... collected 2 items
     <Module 'check_myapp.py'><Class 'CheckMyApp'>
@@ -82,7 +82,7 @@
 
     . $ py.test --collectonly pythoncollection.py
     =========================== test session starts ============================
-    platform linux2 -- Python 2.7.1 -- pytest-2.2.4
+    platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev1
     collecting ... collected 3 items
     <Module 'pythoncollection.py'><Function 'test_function'>
@@ -92,3 +92,55 @@
           <Function 'test_anothermethod'>
     
     =============================  in 0.00 seconds =============================
+
+customizing test collection to find all *.py files
+---------------------------------------------------------
+
+.. regendoc:wipe
+
+You can easily instruct py.test to discover tests from every python file::
+
+
+    # content of pytest.ini
+    [pytest]
+    python_files = *.py
+
+However, many projects will have a ``setup.py`` which they don't want to be imported. Moreover, there may files only importable by a specific python version.
+For such cases you can dynamically define files to be ignored by listing 
+them in a ``conftest.py`` file:: 
+
+    # content of conftest.py
+    import sys
+
+    collect_ignore = ["setup.py"]
+    if sys.version_info[0] > 2:
+        collect_ignore.append("pkg/module_py2.py")
+
+And then if you have a module file like this::
+
+    # content of pkg/module_py2.py
+    def test_only_on_python2():
+        try:
+            assert 0
+        except Exception, e:
+            pass
+
+and a setup.py dummy file like this::
+
+    # content of setup.py
+    0/0  # will raise exeption if imported
+
+then a pytest run on python2 will find the one test when run with a python2 
+interpreters and will leave out the setup.py file::
+    
+    $ py.test --collectonly
+    =========================== test session starts ============================
+    platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev1
+    collecting ... collected 1 items
+    <Module 'pkg/module_py2.py'>
+      <Function 'test_only_on_python2'>
+    
+    =============================  in 0.01 seconds =============================
+
+If you run with a Python3 interpreter the moduled added through the conftest.py file will not be considered for test collection.
+

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the pytest-commit mailing list