mercredi 4 septembre 2019

Python Unit Test - the else block doesn't execute, and if it does there is no logging

I am building a QA system in Python and Selenium. I implemented a cool logger module and I really like it. My system runs fine and logs everything (but only if the assertions are correct). For example, this following test:

def test_mode_check(self):
    """ Tests if the website is in TEST_MODE """

    login_page = LoginPage(self.driver)

    if login_page.is_test_mode() is True:
        assert True
        logger.success(TEST_MODE_SUCCESSFUL)
    else:
        logger.warning(TEST_MODE_FAILURE)
        print("i reach this")
        assert False

A little bit of explanation. TEST_MODE is an environment variable I set for the website, which should be set before the tests. Hence, it checks if it is on. TEST_SUCCESSFUL and TEST_FAILURE are just global variables, that contain strings with the appopriate message. The logger I am using is called 'loguru'. Also login_page.is_test_mode() is just a page-specific method that looks for the TEST_MODE visual indication on the page and returns TRUE if it finds it. This is the method for reference:

def is_test_mode(self):
    """
    This function checks if the website is in test mode. The tests should not proceed if it is not in TEST_MODE.

    :return: True or False depending on the condition
    """

    if self.find_element(*self.locator.IS_TEST_MODE).text == \
            "The website is running in test mode. ":
        return True
    else:
        return False

So, if the env variable is set this is the output:

04/09/19 at 11:41:24 AM | SUCCESS  | TEST_MODE is set ✓


test_mode_check (tests.individual_tests.tests_preliminary.PreliminaryTests) ... OK (5.645s)

Everything is great. However, if I intentionally remove the ENV variable in order to test the error logging, this is the only thing that happens:

test_mode_check (tests.individual_tests.tests_preliminary.PreliminaryTests) ... ERROR (7.585422)
s

======================================================================
ERROR [7.585422s]: tests.individual_tests.tests_preliminary.PreliminaryTests.test_mode_check
...
# here goes the exception ... which basically says that an element is absent
from the webpage

My warning message, which gives more information is not displayed at all. I also tried a print function in the else block print('i reach this'), but nothing displays. Can anyone give me any tips? I can provide more information if needed.

Aucun commentaire:

Enregistrer un commentaire