From fde65270afb3855057a86b40c5996c86f58fe97b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 30 Jul 2012 20:58:59 +1200 Subject: Testing examples and docs. --- libpathod/templates/docs_test.html | 43 ++++++++++++++++++++++++++++ libpathod/templates/examples_context.html | 24 ++++++++++++++++ libpathod/templates/examples_setup.html | 29 +++++++++++++++++++ libpathod/templates/examples_setupall.html | 38 ++++++++++++++++++++++++ libpathod/templates/examples_test.html | 18 ------------ libpathod/templates/index.html | 4 +-- libpathod/templates/request_previewform.html | 2 +- libpathod/test.py | 7 +++++ 8 files changed, 144 insertions(+), 21 deletions(-) create mode 100644 libpathod/templates/examples_context.html create mode 100644 libpathod/templates/examples_setup.html create mode 100644 libpathod/templates/examples_setupall.html delete mode 100644 libpathod/templates/examples_test.html (limited to 'libpathod') diff --git a/libpathod/templates/docs_test.html b/libpathod/templates/docs_test.html index 27129f1c..c9162f87 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.

+ +
+
+

Context Decorator

+ + {% include "examples_context.html" %} +
+
+ +
+
+

One instance per test

+ + {% include "examples_setup.html" %} +
+
+

One instance per suite

+ + {% include "examples_setupall.html" %} +
+
+ + {% endblock %} diff --git a/libpathod/templates/examples_context.html b/libpathod/templates/examples_context.html new file mode 100644 index 00000000..3d33e76d --- /dev/null +++ b/libpathod/templates/examples_context.html @@ -0,0 +1,24 @@ +
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/libpathod/templates/examples_setup.html b/libpathod/templates/examples_setup.html new file mode 100644 index 00000000..bde45840 --- /dev/null +++ b/libpathod/templates/examples_setup.html @@ -0,0 +1,29 @@ +
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/libpathod/templates/examples_setupall.html b/libpathod/templates/examples_setupall.html new file mode 100644 index 00000000..b7f79975 --- /dev/null +++ b/libpathod/templates/examples_setupall.html @@ -0,0 +1,38 @@ +
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/libpathod/templates/examples_test.html b/libpathod/templates/examples_test.html deleted file mode 100644 index 57d39534..00000000 --- a/libpathod/templates/examples_test.html +++ /dev/null @@ -1,18 +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/libpathod/templates/index.html b/libpathod/templates/index.html index c31d4414..b020d60b 100644 --- a/libpathod/templates/index.html +++ b/libpathod/templates/index.html @@ -70,9 +70,9 @@

libpathod.test

-

Using pathod and pathoc in your unit tests.

+

Using pathod in your unit tests.

- {% include "examples_test.html" %} + {% include "examples_context.html" %}
diff --git a/libpathod/templates/request_previewform.html b/libpathod/templates/request_previewform.html index f38758ea..263b501e 100644 --- a/libpathod/templates/request_previewform.html +++ b/libpathod/templates/request_previewform.html @@ -1,6 +1,6 @@