Skip to content

Commit

Permalink
added the hoverpy.TestCase class, and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
shyal committed Nov 30, 2016
1 parent eab1293 commit 2889421
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ python examples/modify/modify.py

Demonstrations how to modify requests. This is particularly useful for sending curved balls to your applications, and make sure they deal with them correctly.

## [unittesting](examples/unittesting)

```bash
env HOVERPY_CAPTURE=true python examples/unittesting/unittesting.py
python examples/unittesting/unittesting.py
```

Demonstrates how to use the `hoverpy.TestCase` class for unit testing purposes.

-------------------------------

![logo](https://github.com/SpectoLabs/hoverfly/raw/master/core/static/img/hoverfly_logo.png)
Expand Down
11 changes: 10 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ Demonstrates how to add latency to calls, based on host, and method type.
python examples/modify/modify.py
```

Demonstrations how to modify requests. This is particularly useful for sending curved balls to your applications, and make sure they deal with them correctly.
Demonstrations how to modify requests. This is particularly useful for sending curved balls to your applications, and make sure they deal with them correctly.

## unittesting

```bash
env HOVERPY_CAPTURE=true python examples/unittesting/unittesting.py
python examples/unittesting/unittesting.py
```

Demonstrates how to use the `hoverpy.TestCase` class for unit testing purposes.
41 changes: 41 additions & 0 deletions examples/unittesting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
In this example, we'll take a look at writing unit tests that use HoverPy. Please note that doing so means that you, as a developer, can be entirely sure that you are testing your code against known data. This makes you hermetic to to issues with third party APIs. Let's begin by importing hoverpy.

```python
import hoverpy

```

Instead of inheriting off `unittest.TestCase` let's inherit off `hoverpy.TestCase`

```python
class TestRTD(hoverpy.TestCase):

```

If our test, we'll once again download a load of readthedocs pages

```python
def test_rtd_links(self):
import requests
limit = 50
sites = requests.get("http://readthedocs.org/api/v1/project/?limit=%d&offset=0&format=json" % limit)
objects = sites.json()['objects']
links = ["http://readthedocs.org" + x['resource_uri'] for x in objects]
self.assertTrue(len(links) == limit)
for link in links:
response = requests.get(link)
print(link, response)
self.assertTrue(response.status_code == 200)

```

Let's run our hoverpy testcase if the script is invoked directly

```python
if __name__ == '__main__':
import unittest
unittest.main()

```

<hr> Now the correct way of launching this script the first time is: <br><br> `$ env HOVERPY_CAPTURE=true python examples/unittesting/unittesting.py`<br><br> which sets HoverPy in capture mode, and creates our all important `requests.db`. This process may take around 10 seconds depending on your internet speed. Now when we're rerun our unit tests, we'll always be running them against the data we captured.<br><br> `$ python examples/unittesting/unittesting.py`<br><br> This time we are done in around 100ms
38 changes: 38 additions & 0 deletions examples/unittesting/unittesting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# In this example, we'll take a look at writing unit tests that use HoverPy.
# Please note that doing so means that you, as a developer, can be entirely sure
# that you are testing your code against known data. This makes you hermetic to
# to issues with third party APIs. Let's begin by importing hoverpy.

import hoverpy

# Instead of inheriting off `unittest.TestCase` let's inherit off
# `hoverpy.TestCase`
class TestRTD(hoverpy.TestCase):

# if our test, we'll once again download a load of readthedocs pages
def test_rtd_links(self):
import requests
limit = 50
sites = requests.get("http://readthedocs.org/api/v1/project/?limit=%d&offset=0&format=json" % limit)
objects = sites.json()['objects']
links = ["http://readthedocs.org" + x['resource_uri'] for x in objects]
self.assertTrue(len(links) == limit)
for link in links:
response = requests.get(link)
print(link, response)
self.assertTrue(response.status_code == 200)

# let's run our hoverpy testcase if the script is invoked directly
if __name__ == '__main__':
import unittest
unittest.main()


# <hr> Now the correct way of launching this script the first time is: <br><br>
# `$ env HOVERPY_CAPTURE=true python examples/unittesting/unittesting.py`<br><br>
# which sets HoverPy in capture mode, and creates our all important `requests.db`.
# This process may take around 10 seconds depending on your internet speed.
# Now when we're rerun our unit tests, we'll always be running them against the
# data we captured.<br><br>
# `$ python examples/unittesting/unittesting.py`<br><br>
# This time we are done in around 100ms.
16 changes: 16 additions & 0 deletions hoverpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ def quick_test():
if "data" in hp.delays().keys():
print("HOVERPY AND HOVERFLY QUICK TEST SUCCESS!!")

import unittest

class TestCase(unittest.TestCase):

hp = None

def setUp(self):
enabled = os.environ.get("HOVERPY_ENABLED", "true").lower() in ["true", "1", "on"]
if enabled:
capture = os.environ.get("HOVERPY_CAPTURE", "").lower() in ["true", "1", "on"]
self.hp = HoverPy(capture=capture)

def tearDown(self):
if self.hp:
self.hp.disableProxy()

if __name__ == "__main__":
quick_test()

3 changes: 2 additions & 1 deletion hoverpy/generateDocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"examples/delays/delays.py",
"examples/basic/basic.py",
"examples/modify/modify.py",
"examples/readthedocs/readthedocs.py"
"examples/readthedocs/readthedocs.py",
"examples/unittesting/unittesting.py",
]

for example in examples:
Expand Down

0 comments on commit 2889421

Please sign in to comment.