[py-svn] r63329 - in py/extradoc/talk/pycon-us-2009/pytest-advanced: . example

hpk at codespeak.net hpk at codespeak.net
Thu Mar 26 01:48:28 CET 2009


Author: hpk
Date: Thu Mar 26 01:48:26 2009
New Revision: 63329

Modified:
   py/extradoc/talk/pycon-us-2009/pytest-advanced/example/test_funcarg.py
   py/extradoc/talk/pycon-us-2009/pytest-advanced/pytest-advanced.txt
Log:
intermediate


Modified: py/extradoc/talk/pycon-us-2009/pytest-advanced/example/test_funcarg.py
==============================================================================
--- py/extradoc/talk/pycon-us-2009/pytest-advanced/example/test_funcarg.py	(original)
+++ py/extradoc/talk/pycon-us-2009/pytest-advanced/example/test_funcarg.py	Thu Mar 26 01:48:26 2009
@@ -1,9 +1,21 @@
+import py
+
+def pytest_addoption(parser):
+    parser.addoption("--hello", dest="hello", action="store", default=None)
 
 def pytest_configure(config):
-    config.register_funcargmaker("myarg", myargmaker)
+    config.register_funcargmaker("myarg", MyArg(config.option.hello).make)
+
+def pytest_unconfigure(config):
+    config.register_funcargmaker("myarg", MyArg(config.option.hello).make)
 
-def myargmaker(pyfuncitem):
-    return pyfuncitem.name 
+class MyArg:
+    def __init__(self, hello):
+        self.hello = hello
+    def make(self, pyfuncitem):
+        if pyfuncitem.config.option.hello == "skip":
+            py.test.skip("could not configure ...")
+        return 42
 
-def test_somefunction(myarg2):
-    assert myarg == 'test_somefunction'
+def test_somefunction(myarg):
+    assert myarg.hello == "some"

Modified: py/extradoc/talk/pycon-us-2009/pytest-advanced/pytest-advanced.txt
==============================================================================
--- py/extradoc/talk/pycon-us-2009/pytest-advanced/pytest-advanced.txt	(original)
+++ py/extradoc/talk/pycon-us-2009/pytest-advanced/pytest-advanced.txt	Thu Mar 26 01:48:26 2009
@@ -195,24 +195,35 @@
 basic funcarg mechanism 
 ==========================================
 
-::
+- provide a "funcarg" maker 
+- and call it for each test function that uses it
 
- def pytest_configure(config):
-   config.register_funcargmaker(
-        "myarg", myargmaker)
 
- def myargmaker(pyfuncitem):
-   return pyfuncitem.name 
+Example test function
+==========================================
+::
 
  def test_somefunction(myarg):
    assert myarg == 'test_somefunction'
 
+
+Example conftest.py 
+==========================================
+:: 
+
+ class ConftestPlugin:
+   def pytest_configure(self, config):
+     config.register_funcargmaker("myarg", myargmaker)
+
+   def myargmaker(self, pyfuncitem):
+     return pyfuncitem.name 
+
 observations
 =====================
 
 funcargs:
 
-* test functions receive value by using a name
+* test functions receive value by specifying a name
 * makers are registered after command line parsing 
 * makers have meta-access to "collected function item" 
 



More information about the pytest-commit mailing list