0

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}'}]))
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
AK1
  • 1
  • 2

1 Answers1

0

You definitely need to read more about pytest fixtures here.

You are not allowed to call fixtures as usual methods. They must be "requested" by tests or other fixtures.

One of the possible solutions of your problem is to pass your WebSocket connection object (ws variable) to ABC class __init__ method.

So the code for ABC class will look like:

class ABC:
    def __init__(self, ws):
        self.ws = ws

    def response(self, status):
        self.ws.send(json.dumps([3, 12345, {"status": f'{status}'}]))
        # ... your code

setup fixture must yield your ws object. Use yield ws in your fixture instead of bare yield. Also using global and autouse=True are not necessary.

Test methods will request your setup fixture and pass it's value to ABC constructor like this:

def test_case1(setup):
    tc = ABC(setup)
    tc.response("Accepted")

Please take some time to learn more about using fixtures.

pL3b
  • 1,155
  • 1
  • 3
  • 18
  • Thanks for the reply @pL3b I'm new in pytest and currently learning. The solution provided by you is working if I'm calling one method in a test method but To make a flow i have to call multiple methods in one test method, if I'm doing this, first method is working fine, But the second method which is being called is fetching the request data of first method. like this:- – AK1 Mar 10 '23 at 05:01
  • like this:- def test_Flow(setup): tc = ABC(setup) tc1 = ABC1(setup) tc.request("Example") tc.response("Accepted") tc1.request1("Example1") tc1.request2("Example2") – AK1 Mar 10 '23 at 05:08
  • well, if `create_connection` returns just one-request connection then may be you should not use a fixture, but just an usual python method or a context manager. – pL3b Mar 10 '23 at 09:40