Pytest with Background Thread Fixtures
Tags: threads, testing, pytest, development
Published: 2018-12-08 23:04:47

---
title: Pytest with Background Thread Fixtures
published: true
tags: threads,testing,pytest,development
canonical_url: https://medium.com/@hmajid2301/pytest-with-background-thread-fixtures-f0dc34ee3c46
---

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F5xrkJe3IJKSze%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Frad-sooziq-capture-5xrkJe3IJKSze&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F5xrkJe3IJKSze%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="351" frameborder="0" scrolling="no"><a href="https://medium.com/media/f288938e0d12ec115f481acb0f5d9d45/href">https://medium.com/media/f288938e0d12ec115f481acb0f5d9d45/href</a></iframe>

Recently I had to test some of my Python 🐍🐍🐍 code which required an external dependency and communicating by using TCP sockets 🔌 . You can think of this dependency as essentially a database because it stored information. However, when testing my Python code, I couldn’t rely on there always being a TCP server to send messages to.

So I ended up creating a simplified mocked version in Python. This way I could run automated tests on my code without needing to either install/rely on this server already existing there. Essentially I ended up creating a TCP server in Python, which would receive some input and respond how I would expect the real TCP server too.

I write my tests using Pytest for several reasons, I prefer it to unittest because it required less boilerplate code. It also has lots of useful features such as fixtures . However, when creating tests for my API I realised that I had to first start the fake TCP server. But since it was always listening 👂 for traffic 🚦it would block the rest of my tests from running. So in this example, I will show you how you can create a Pytest fixture and run it on a background thread (TCP server) and still run your tests as normal.

### tcp\_server.py

![](https://cdn-images-1.medium.com/max/800/1*2H1Kvh6RfkFtMUh-fXJuEg.png)<figcaption>tcp_server.py</figcaption>

This is a very basic TCP server, it received some input but always replies with “Received”. Also, I created the class to be used with the context manager so that the socket can be closed, hence the _\_\_enter\_\_()_ and _\_\_exit\_\_()_ dunder (magic) method. This TCP server will always listen on localhost on port 9500, this is the same port the client will also have to connect to, to send messages to this TCP server.

The main function of interest is _listen\_for\_traffic()_, which loops forever whilst listening for traffic . If it receives any data, it always replies with “Received”, of course with a real server we might want to do something more interesting but to keep this example simple that’s all this server will do.

### test\_example.py

![](https://cdn-images-1.medium.com/max/1024/1*PfeX25OzVkHyYoa4Ffv00A.png)<figcaption>test_example.py</figcaption>

When you run the Pytest command it will automatically run tests on all modules and method that start with “test\_”. I have created a fixture (using the fixture decorator), fixtures allow for code reuse within a Pytest module. Fixtures are typically used to connect to databases, fixtures are the run before any tests hence we can also use them to setup our code.

So taking a look at the _dummy\_tcp\_server()_ function, first we use a context manager (with) to set up our TCP server.

```
with example_server as tcp_server:
```

This calls the _\_\_enter\_\_()_ dunder method, which starts up our TCP server and so it’s now listening for any traffic on port 9500.

```
thread = threading.Thread(target=example_server.listen_for_traffic)
thread.daemon = True
thread.start()
```

This is the bit of code that actually runs our TCP server on another (background) thread. So the in the first line we tell the thread what function to call, _listen\_for\_traffic_ (without brackets) has an infinite while loops just waiting to listen for traffic arriving on port 9500. We set the thread as a daemon so that when we close the main Python program the thread will automatically kill itself, this means we don’t have to manage it yourself.

```
yield example_server
```

The yield is very similar to a return statement. The main reason I choose to include is that if the tests for some reason raise an exception, the yield will still allow the context manager to exit. If we had any tear down code after the yield even with an exception, this code will still run after all the tests have finished. Also yields (and return statements) allow individual tests to access functions from the TCPServer class. For example, if you had a function (in the TCPServer class) that stored every message sent to the server. If you pass the fixture as a parameter to any tests then you could access this function.

```
def test_example():
 HOST = '127.0.0.1'
 PORT = 9500

 data = ""
 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
 s.connect((HOST, PORT))
 s.sendall(b'Hello World')
 data = s.recv(1024)

 assert data.decode() == "Received"
```

The part of our code is the actual test. It sends “Hello World” to the TCP server, remember if you sent it as string (without _b’’_) then you’ll have to encode that string first (_“Hello World”.encode()_). Then we capture the response from the server and decode it (because it’s in binary, this converts it into a string) and assert or check that the data is the string “Received” as we expect.

### Appendix

- [Example source code](https://github.com/hmajid2301/medium/tree/master/6.%20Pyest%20with%20Background%20Threads)
- [Pytest](https://docs.pytest.org/en/latest/)
- [Code images made with carbon](https://carbon.now.sh)