0

I have the below code:

master.py    

def create():
    master = falcon.API(middleware=Auth())
    msg = Message()
    master.add_route('/message', msg)


master = create()

if __name__ == '__main__':
    httpd = simple_server.make_server("127.0.0.1", 8989, master)
    process = Thread(target=httpd.serve_forever, name="master_process")
    process.start()

    #Some logic happens here
    s_process = Thread(target=httpd.shutdown(), name="shut_process")
    s_process.start()
    s.join()

I tried to create the below test case for the below:

from falcon import testing
from master import create

@pytest.fixture(scope='module')
def client():
   return testing.TestClient(create())

def test_post_message(client):
   result = client.simulate_post('/message', headers={'token': "ubxuybcwe"}, body='{"message": "I'm here!"}') --> This line throws the error
   assert result.status_code == 200

I tried running the above but get the below error:

TypeError: 'NoneType' object is not callable

I actually am unable to figure out how I should go about writing the test case for this.

Rajan
  • 1,463
  • 12
  • 26
user1452759
  • 8,810
  • 15
  • 42
  • 58

1 Answers1

0

Based on what @hoefling said, the below fixed it:

master.py    

def create():
     master = falcon.API(middleware=Auth())
     msg = Message()
     master.add_route('/message', msg)
     return master


master = create()


if __name__ == '__main__':
    httpd = simple_server.make_server("127.0.0.1", 8989, master)
    process = Thread(target=httpd.serve_forever, name="master_process")
    process.start()

    #Some logic happens here
    s_process = Thread(target=httpd.shutdown(), name="shut_process")
    s_process.start()
    s.join()

And then the test case works:

from falcon import testing
from master import create

@pytest.fixture(scope='module')
def client():
    return testing.TestClient(create())

def test_post_message(client):
    result = client.simulate_post('/message', headers={'token': "ubxuybcwe"}, 
    body='{"message": "I'm here!"}') 
    assert result.status_code == 200

Thanks a lot @hoefling!

user1452759
  • 8,810
  • 15
  • 42
  • 58