I have a fixture in conftest.py:
@pytest.fixture(scope="session", autouse=True)
def setup( server ):
global ws
if server == "Test1":
ws = create_connection(base_class.server1)
print("Websocket Connection Established successfully")
elif server == "Test2":
ws = create_connection(base_class.server2)
print("Websocket Connection Established successfully")
elif server == "Test3":
ws = create_connection(base_class.server3)
print("Websocket Connection Established successfully")
yield
ws.close()
print("Websocket Connection Closed successfully")
I'm using POM model and my methods are defined under a class like this in a separate file:
class ABC:
def response(self, status):
ws.send(json.dumps([3, 12345, {"status": f'{status}'}]))
I have a separate file test file in which test cases are defined like:
tc = ABC()
test_case1(setup):
tc.response("Accepted")
It is throwing NameError:
ws.send(json.dumps([3, requestid, {"status": f'{status}'}]))
E NameError: name 'ws' is not defined
How to get the value of ws from the fixture in methods defined under the class ABC?
I've also used the fixture for the class, but it is throwing an error, fixture cannot be called directly.
@pytest.mark.usefixture(name=setup)
class ABC:
def response(self, status):
ws.send(json.dumps([3, 12345, {"status": f'{status}'}]))