| id | samples-flask | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| title | Sample Task Creation CRUD App | ||||||||
| sidebar_label | Flask + Mongo | ||||||||
| description | The following sample app showcases how to use the Flask framework and the Keploy Platform. | ||||||||
| tags |
|
||||||||
| keyword |
|
import Link from '@docusaurus/Link' import InstallReminder from '@site/src/components/InstallReminder'; import SectionDivider from '@site/src/components/SectionDivider';
🪄 Dive into the world of Student CRUD Apps and see how seamlessly Keploy integrated with Flask and MongoDB Buckle up, it's gonna be a fun ride! 🎢
git clone https://github.com/keploy/samples-python.git && cd samples-python/flask-mongoWe will be using Docker compose to run the application as well as Mongo on Docker container.
Capture the test-cases-
keploy record -c "docker compose up" --container-name "flask-app" --buildDelay 50🔥Make some API calls. Postman, Hoppscotch or even curl - take your pick!
Let's make URLs short and sweet:
To generate testcases we just need to make some API calls.
- Make a POST request:
curl -X POST -H "Content-Type: application/json" -d '{"student_id": "12345", "name": "John Doe", "age": 20}' http://localhost:6000/students- Make a GET request:
curl http://localhost:6000/students- Make a PUT request:
curl -X PUT -H "Content-Type: application/json" -d '{"name": "Jane Smith", "age": 21}' http://localhost:6000/students/12345- Make a DELETE request:
curl -X DELETE http://localhost:6000/students/12345And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the keploy directory and you'll discover your handiwork in tests directory and mocks.yml.
Want to see if everything works as expected?
Time to put things to the test 🧪
keploy test -c "docker compose up" --container-name "flask-app" --buildDelay 50 --delay 10The
--delayflag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking.
Happy coding! ✨👩💻👨💻✨
🪄 Dive into the world of Student CRUD Apps and see how seamlessly Keploy integrated with Flask and MongoDB Buckle up, it's gonna be a fun ride! 🎢
git clone https://github.com/keploy/samples-python.git && cd samples-python/flask-mongoWe'll be running our sample application right on Linux, but just to make things a tad more thrilling, we'll have the database (MongoDB) chill on Docker. Ready? Let's get the party started!🎉
pip install -r requirements.txtdocker run -p 27017:27017 -d --network backend --name mongo mongoSince we are using a MongoDB container, we need to update the
clienton line 11 inapp.py, tolocalhost.
To initiate the recording of API calls, execute this command in your terminal:
keploy record -c "python3 app.py"Now, your app will start running, and you have to make some API calls to generate the test cases!!
- Make a POST request:
curl -X POST -H "Content-Type: application/json" -d '{"student_id": "12345", "name": "John Doe", "age": 20}' http://localhost:6000/students- Make a GET request:
curl http://localhost:6000/students- Make a PUT request:
curl -X PUT -H "Content-Type: application/json" -d '{"name": "Jane Smith", "age": 21}' http://localhost:6000/students/12345- Make a DELETE request:
curl -X DELETE http://localhost:6000/students/12345And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the keploy directory and you'll discover your handiwork in tests directory and mocks.yml.
Now, it's time to put things to the test 🧪
keploy test -c "python3 app.py" --delay 10Now, you can also try different API calls, tweak the DB response in the mocks.yml, or fiddle with the request or response in test-x.yml. Run the tests again and see the magic unfold!
We have a test-app.py where all the unit test cases has been written. Now using Keploy, we can check it's code coverage!!
Now to run your unit tests with Keploy, you can run the command given below:
python3 -m coverage run -p --data-file=.coverage.unit -m pytest -s test_keploy.py test_app.pyTo combine the coverage from the unit tests, and Keploy's API tests we can use the command below:
python3 -m coverage combineFinally, to generate the coverage report for the test run, you can run:
python3 -m coverage reportand if you want the coverage in an html file, you can run:
python3 -m coverage htmlCongrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.😊🚀