From 44abdc9391a058f2709e1946e1892d04f0eec33c Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 28 Apr 2009 19:49:48 +0200 Subject: [PATCH] add an example for lazy "per-directory" setup. --HG-- branch : trunk --- example/funcarg/lazysetup/conftest.py | 22 ++++++++++++++++++++++ example/funcarg/lazysetup/sub1/test_one.py | 6 ++++++ example/funcarg/lazysetup/sub2/test_two.py | 3 +++ 3 files changed, 31 insertions(+) create mode 100644 example/funcarg/lazysetup/conftest.py create mode 100644 example/funcarg/lazysetup/sub1/test_one.py create mode 100644 example/funcarg/lazysetup/sub2/test_two.py diff --git a/example/funcarg/lazysetup/conftest.py b/example/funcarg/lazysetup/conftest.py new file mode 100644 index 000000000..760491e5c --- /dev/null +++ b/example/funcarg/lazysetup/conftest.py @@ -0,0 +1,22 @@ + +class ConftestPlugin: + def pytest_configure(self, config): + self._setup = None + + def pytest_funcarg__setup(self, request): + if self._setup is None: + self._setup = LazySetup() + return self._setup + + def pytest_unconfigure(self, config): + if self._setup is not None: + self._setup.finalize() + +class LazySetup: + def __init__(self): + import time + time.sleep(5) + self.timecostly = 1 + + def finalize(self): + del self.timecostly diff --git a/example/funcarg/lazysetup/sub1/test_one.py b/example/funcarg/lazysetup/sub1/test_one.py new file mode 100644 index 000000000..a3ca9f218 --- /dev/null +++ b/example/funcarg/lazysetup/sub1/test_one.py @@ -0,0 +1,6 @@ + +def test_something(setup): + assert setup.timecostly == 1 + +def test_something_more(setup): + assert setup.timecostly == 1 diff --git a/example/funcarg/lazysetup/sub2/test_two.py b/example/funcarg/lazysetup/sub2/test_two.py new file mode 100644 index 000000000..753b0e9c4 --- /dev/null +++ b/example/funcarg/lazysetup/sub2/test_two.py @@ -0,0 +1,3 @@ + +def test_quick(): + pass