Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform unit test for Falcon API python

I have developed an application using Falcon framework in Python language. I developed few API, Now I wanted to perform unit test so I used unittest package, But I am not able to test my APIs as it fails unittest. For testing I refer this doc I tried to simulate the get method but it asking me argument. Also I am unable to perform any put or post testing. Here are my code to perform unit test

main.py

from app.api.v1 import scheduler
self.add_route('/v1/schedulers', scheduler.SchedulerCollection())

scheduler.py

class SchedulerCollection(BaseResource):

def __init__(self):
    db = redis_db.RedisStorageEngine()
    self.r = db.connection()

"""
Handle for endpoint: /v1/schedulers
"""

# @falcon.before(validate_scheduler_create) def on_post(self, req, res):

    #some code here

#@falcon.before(auth_required)
def on_get(self, req, res):

    #some code here

scheduler_test.py

import sys, os
import unittest
import json
from falcon import testing
from app.api.v1 import scheduler

class SchedulerTestCase(testing.TestCase):
    def setUp(self):
        super(SchedulerTestCase, self).setUp()
        self.app = scheduler.SchedulerCollection.on_get 

class TestMyApp(SchedulerTestCase):
    def test_get_schedulers(self):
        doc = {u'message': u'Hello world!'}
        result = self.simulate_get('/v1/schedulers')
        result = self.simulate_get('/v1/schedulers')
        self.assertEqual(result.json, doc)




if __name__ == '__main__':
        unittest.main()

After running test I am getting this error

Traceback (most recent call last):
  File "tests/scheduler_test.py", line 17, in setUp
    self.app = scheduler.SchedulerCollection().on_get(self.api_class)
TypeError: on_get() takes exactly 3 arguments (2 given)

I don't know where I am doing wrong

Any help would be highly appreciated

Thanks

like image 761
Amit Sharma Avatar asked Oct 15 '25 18:10

Amit Sharma


1 Answers

First off, self.app should be initialize with your falcon app, not the method you are testing. Secondly, simulate_get is an attribute of self.app (not self).

Check more info at this answer.

like image 110
joarleymoraes Avatar answered Oct 17 '25 08:10

joarleymoraes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!