iollo
iollo

Reputation: 47

Generate Log Python

I am new to coding and I did not understand why I cannot generate logs after my test case is done.

Here is the code for generating log



class LogGen:
    @staticmethod
    def loggen():
        logging.basicConfig(filename=".\\Logs\\automation.log",
                            format='%(asctime)s: %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)
        return logger

And here is the code where I call this method:

from selenium import webdriver
from pageObjects.storetest import store_test
from utilities.readProperties import ReadConfig
from selenium.webdriver.common.keys import Keys
import time
from utilities.customeLogger import LogGen


class Test_001_learning:
    baseURL = ReadConfig.getApplicationURL()
    email = ReadConfig.getUseremail()
    password = ReadConfig.getPassword()
    logger = LogGen.loggen()
    PURCHASEPRICE = "100"
    DOWNPAYMENT = "10"
    HOMEINSURANCE = "10"
    PROPERTYTAXES = "10"
    HOADUES = "10"

    def test_store(self, setup):
        self.logger.info("********* Test_001_Learning ********")
        self.logger.info("********* LOggin test  ********")
        self.driver = setup
        self.driver.get(self.baseURL)
        self.driver.get(self.baseURL)
        self.lp = store_test(self.driver)
        self.lp.setemail(self.email)
        self.lp.setpassword(self.password)
        self.lp.button1()
        title = self.driver.title
        if title == "Dashboard / nopCommerce administration":
            assert True
            self.logger.info("********* Test Pass ********")
        else:
            self.driver.save_screenshot(".\\Screenshots\\" + "test_calculator2.png")
            assert False

        self.lp.button2()
        self.driver.close()

But after the test case is done no logs are generated.

Thank you in advanced!

Upvotes: 1

Views: 559

Answers (1)

hoefling
hoefling

Reputation: 66521

By default, pytest overwrites any custom logging configuration with its own one in order to capture any logs emitted in the test run; this is useful to test whether your application emits the logs correctly. You can disable this functionality by turning off the logging plugin:

$ pytest -p no:logging ...

Now no logs will be captured and your own logging configuration is applied.

Alternative: live logs

However, pytest also has a neat feature of live logging; when enabled, it will capture the logs and write them to terminal or file or both. You can thus replace LogGen with the configuration done in pytest.ini or pyproject.toml. Example with pyproject.toml:

[tool.pytest.ini_options]
log_file = "Logs/automation.log"
log_file_level = "INFO"
log_file_format = "%(asctime)s: %(levelname)s: %(message)s"
log_file_date_format = "%m/%d/%Y %I:%M:%S %p"

In the tests, just use the root logger; no need to configure anything in code:

class Test_001_learning:

    def test_store(self):
        self.logger = logging.getLogger()
        self.logger.info("********* Test_001_Learning ********")
        ...

Shameless self-promotion: check out my other answer for more configuration examples.

Upvotes: 2

Related Questions