Reputation: 181
I am using pytest and django (pytest-django) to write a test case that test some client operation to the service.
The test case first created some db models with fixtures. Then it will make a http call to the code being tested. Due to a complexity of the client code, I don't want to execute the service code directly, I want the client to sent a http request and then execute the server code from the request handler.
For this I have another fixture that is essentially a simple HTTP Threaded server that will listen to port 8000 and handle the request.
However, the threaded server doesn't have access to the DB because it is in another thread and not in the same transaction as the test case.
How can I let the httpd fixture have access to the testcase transaction data. I have tried add another layer of transaction.atomic before yielding (as gpt suggested) but it does not work
in the httpd fixture, I have this....I am not sure why it doesn't work
with transaction.atomic():
httpd = UnitTestHTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
server_thread = SimpleHTTPServerThread(httpd=httpd)
server_thread.start()
yield httpd
# Shutdown the server and join the thread after the test
server_thread.join()
Upvotes: 0
Views: 19