diff --git a/.sources/examples_context.py b/.sources/examples_context.py new file mode 100644 index 000000000..80278e760 --- /dev/null +++ b/.sources/examples_context.py @@ -0,0 +1,23 @@ +import requests +from libpathod import test + +class Test: + """ + Testing the requests module with + a pathod context manager. + """ + def test_simple(self): + # Start pathod in a separate thread + with test.Daemon() as d: + # Get a URL for a pathod spec + url = d.p("200:b@100") + # ... and request it + r = requests.put(url) + + # Check the returned data + assert r.status_code == 200 + assert len(r.content) == 100 + + # Check pathod's internal log + log = d.last_log()["request"] + assert log["method"] == "PUT" diff --git a/.sources/examples_setup.py b/.sources/examples_setup.py new file mode 100644 index 000000000..5366c9ef1 --- /dev/null +++ b/.sources/examples_setup.py @@ -0,0 +1,28 @@ +import requests +from libpathod import test + +class Test: + """ + Testing the requests module with + a pathod instance started for + each test. + """ + def setUp(self): + self.d = test.Daemon() + + def tearDown(self): + self.d.shutdown() + + def test_simple(self): + # Get a URL for a pathod spec + url = self.d.p("200:b@100") + # ... and request it + r = requests.put(url) + + # Check the returned data + assert r.status_code == 200 + assert len(r.content) == 100 + + # Check pathod's internal log + log = self.d.last_log()["request"] + assert log["method"] == "PUT" diff --git a/.sources/examples_setupall.py b/.sources/examples_setupall.py new file mode 100644 index 000000000..c8948971c --- /dev/null +++ b/.sources/examples_setupall.py @@ -0,0 +1,37 @@ +import requests +from libpathod import test + +class Test: + """ + Testing the requests module with + a single pathod instance started + for the test suite. + """ + @classmethod + def setUpAll(cls): + cls.d = test.Daemon() + + @classmethod + def tearDownAll(cls): + cls.d.shutdown() + + def setUp(self): + # Clear the pathod logs between tests + self.d.clear_log() + + def test_simple(self): + # Get a URL for a pathod spec + url = self.d.p("200:b@100") + # ... and request it + r = requests.put(url) + + # Check the returned data + assert r.status_code == 200 + assert len(r.content) == 100 + + # Check pathod's internal log + log = self.d.last_log()["request"] + assert log["method"] == "PUT" + + def test_two(self): + assert not self.d.log() diff --git a/.sources/examples_test.py b/.sources/examples_test.py deleted file mode 100644 index 1f2630e03..000000000 --- a/.sources/examples_test.py +++ /dev/null @@ -1,17 +0,0 @@ -import requests -from libpathod import test - -class Test: - def setUp(self): - self.daemon = test.Daemon() - - def tearDown(self): - self.daemon.shutdown() - - def test_simple(self): - path = self.daemon.p("200:b@100") - r = requests.get(path) - assert r.status_code == 200 - assert len(r.content) == 100 - log = self.daemon.last_log() - assert log["request"]["method"] == "GET" diff --git a/.sources/make b/.sources/make index c7208bf47..e0e9d2cc0 100755 --- a/.sources/make +++ b/.sources/make @@ -1,2 +1,4 @@ #!/bin/sh -pygmentize -f html ./examples_test.py > ../libpathod/templates/examples_test.html +pygmentize -f html ./examples_context.py > ../libpathod/templates/examples_context.html +pygmentize -f html ./examples_setup.py > ../libpathod/templates/examples_setup.html +pygmentize -f html ./examples_setupall.py > ../libpathod/templates/examples_setupall.html diff --git a/libpathod/templates/docs_test.html b/libpathod/templates/docs_test.html index 27129f1cc..c9162f877 100644 --- a/libpathod/templates/docs_test.html +++ b/libpathod/templates/docs_test.html @@ -6,4 +6,47 @@ Using pathod and pathoc in your unit tests. + +
The libpathod.test module is a light, flexible testing layer + for HTTP clients. It works by firing up a Pathod instance in a separate + thread, letting you use Pathod's full abilities to generate responses, + and then query Pathod's internal logs to establish what happened. All + the mechanics of startup, shutdown, finding free ports and so forth are + taken care of for you.
+ +The canonical docs can be accessed using pydoc:
+ +pydoc libpathod.testing+ +
The remainder of this page demonstrates some common interaction + patterns using nose. These examples + are also applicable with only minor modification to most commonly used + Python testing engines.
+ +import requests
+from libpathod import test
+
+class Test:
+ """
+ Testing the requests module with
+ a pathod context manager.
+ """
+ def test_simple(self):
+ # Start pathod in a separate thread
+ with test.Daemon() as d:
+ # Get a URL for a pathod spec
+ url = d.p("200:b@100")
+ # ... and request it
+ r = requests.put(url)
+
+ # Check the returned data
+ assert r.status_code == 200
+ assert len(r.content) == 100
+
+ # Check pathod's internal log
+ log = d.last_log()["request"]
+ assert log["method"] == "PUT"
+
import requests
+from libpathod import test
+
+class Test:
+ """
+ Testing the requests module with
+ a single pathod instance started
+ for the test suite.
+ """
+ @classmethod
+ def setUpAll(cls):
+ cls.d = test.Daemon()
+
+ @classmethod
+ def tearDownAll(cls):
+ cls.d.shutdown()
+
+ def setUp(self):
+ # Clear the pathod logs between tests
+ self.d.clear_log()
+
+ def test_simple(self):
+ # Get a URL for a pathod spec
+ url = self.d.p("200:b@100")
+ # ... and request it
+ r = requests.put(url)
+
+ # Check the returned data
+ assert r.status_code == 200
+ assert len(r.content) == 100
+
+ # Check pathod's internal log
+ log = self.d.last_log()["request"]
+ assert log["method"] == "PUT"
+
+ def test_two(self):
+ assert not self.d.log()
+
Using pathod and pathoc in your unit tests.
+Using pathod in your unit tests.
- {% include "examples_test.html" %} + {% include "examples_context.html" %}