Category : unit-testing

I have a dummy funtion to try exceptions: def fun(n): try: if n <1: raise ValueError return 1 except: pass In my unit test I use: import unittest class TestFibonnacci(unittest.TestCase): def test_values(self): self.assertEqual(fun(1),1) self.assertRaises(ValueError, fun(-1)) However I’m unable to get an answer I actually get: An exception occurred E…. ERROR: test_values (main.TestFibonnacci) Traceback (most recent ..

Read more

While trying to improve code coverage of unit tests in my python flask app I am encountering some code coverage gaps. So there is a 404 error handler @app.errorhandler(404) def page_not_found(e): app.logger.exception(e) if isinstance(e, HTTPException): return e return "404 Page not Found", 404 With this unit test def test_404_check(app, client): res = client.get(‘/testof404’) assert res.status_code ..

Read more

I am using firestore with python to write cloud functions. I need to test a function with following query: from google.cloud import firestore MY_COLLECTION_GROUP = firestore.Client(project="my-project").collection_group("my_collection") def function_to_test(data): my_query = ( MY_COLLECTION_GROUP.where("eventDateTime", "==", data["dateTime"]) .where("user.id", "==", data["user_id"]) .where("user.age", ">=", data["valid_age"]) ) # fetch results from query and proceed further To test the above function, I ..

Read more

It looks like garbage collection is a problem with python3 -m unittest discover. Look at this example: file: model.py # a basic SQLAlchemy declarative model, as well as the DB manager to insert the data from sqlalchemy.engine import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy import Table, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base Base ..

Read more

In unit tests, I’m used to writing def patched_function(): … @patch("package.module.function", new=patched_function) class TestExample: And then any time the code I’m testing calls package.module.function, it instead called patched function. But what if I want to patch an entire class? I know about Mock, but that won’t work because it’s the code I’m testing that’s instantiating ..

Read more