'Will a python unit-test actually execute the code being tested and preform those actions (example: add to db)?

I am testing some code (aws lambdas, but I guess this is tech agnostic) the main problem I keep running into is that I cant write code in my test to run code from another function and that adds to a db table. But when I put those same lines of code but in my test file, it will add to db fine.

So if I have a py file like this (the file I want to test):

def lambda_handler(event, context):
    my_db = db_conn("some db user", "some db name")
    my_db.put_to_db_table("Texas", "Austin", "123 Main street")

and my test file look like this:

@pytest.mark.usefixtures('lambda_a_event')
class TestDbStuff(unittest.TestCase):
    def setUp(self):
        self.handler = lambda_handler
        self.sample_event = self.lambda_a_event

    def test_db_lambda_handler(self):
        self.handler(self.sample_event, context=None)

Here shoudln't the self.handler(self.sample_event, context=None) be enough to run and execute the code that is inside of lambda_handler (i.e. add to db)?

I keep running into this issue and just want to confirm whether or not this is what tests can do.

If instead in the test file I put this instead, then it saves to db fine:

@pytest.mark.usefixtures('lambda_a_event')
class TestDbStuff(unittest.TestCase):
    def setUp(self):
        self.handler = lambda_handler
        self.sample_event = self.lambda_a_event

    def test_db_lambda_handler(self):
        my_db = db_conn("some db user", "some db name")
        my_db.put_to_db_table("Texas", "Austin", "123 Main street")

        self.handler(self.sample_event, context=None)

What is causing this? Am I misunderstanding something?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source