Pytest with Background Thread Fixtures

All cool people test their code

tcp_server.py

tcp_server.py

test_example.py

test_example.py
with tcp_server as example_server:
thread = threading.Thread(target=example_server.listen_for_traffic)
thread.daemon = True
thread.start()
yield example_server
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"

--

--

--

Software Engineer | Pythonista | Typescripter | Docker Advocate | https://haseebmajid.dev

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CS373 Spring 2022: Week 13 Ting-Yin Chang Chien

Saving Time with Python and Tableau

Automation using Robot Framework

Sonar Sweeper: Day 1: Advent of Code 2021 — Python Solution

CS373 Spring 2022: Nisha Ramesh

Learn to code By Making 2.5 D Space Shooter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Haseeb Majid

Haseeb Majid

Software Engineer | Pythonista | Typescripter | Docker Advocate | https://haseebmajid.dev

More from Medium

Docker based Postgres DB — docker-compose.yaml

Efficient CPU-intensive GRPC service in Python — Part 2

Connecting a React App to FastAPI

Connecting a React App to FastAPI

An Amateur Approach to JSON Mock Server and Local Data Storage for API Testing