Showing posts with label pytest. Show all posts
Showing posts with label pytest. Show all posts

Monday, October 10, 2022

pytest logging into logfile and html reports

 pypi.org/project/pytest-html/

----

pip install pytest-html

py.test --html=report.html


to add console logs to a log file:

log line standard, timestamp : type_of_error/loglevel : testcasename: message

error type: Debug,Info,warning,error,Critical

test_logging.py

import logging

def test_logging:
 logger=logging.getLogger(__name__)

 fileHandler=logger.FileHandler('logfile.log')
 formatter = logging.Formatter("%(asctime)s :%(levelname)s : %(name)s :%(message)s")
 fileHandler.setFormatter(formatter)
 logger.addHandler(fileHandler) #file handler object is passed
 ## setting log level, so below loglelvels will be ignored.say setting error will ignore  debug,info warning.
 logger.setlevel(logging.INFO)
 logger.debug("A debug statement is executed")
 logger.info("infomration statement")
 logger.warning("Something is in warning mode")
 logger.error("A major error has happened")
 logger.critical("Critical issue")


to add console logs using above code to exiting test cases:

BaseClass.py


import logging
class BaseClass:
 def getLogger(self):
   loggerName = inspect.stack()[1][3]
   logger = logging.getLogger(loggerName) ## as __name__ will print this baseclass name
   fileHandler=logger.FileHandler('logfile.log')
   formatter = logging.Formatter("%(asctime)s :%(levelname)s : %(name)s :%(message)s")
   logger.addHandler(fileHandler)
   logger.setlevel(logging.DEBUG)
   return logger

test_fixtureData.py
import pytest
from pytestsDemo.BaseClass import BaseClass

@pytest.mark.usefixtures("dataLoad")
class TestExample2(BaseClass):
  def test_editProfile(self,dataLoad):
    log = self.getLogger()
    log.info(dataLoad[0])
    log.info(dataLoad[2])

when we generate pytest with html reports, this logger object info will be embedded automatically.

How to install pytest, naming convention and running tests individually,in a folder, filter with filename, filter with testcase name, filter base on a group

 to install pytest:

pip install pytest


naming convention:

file name and testcase/method name: test_***.py


arguments:

-k method names execution

-s logs in output

-v for more info


sample testcase file with multiple test cases with above naming convention followed.


test_demo2.py

---

def test_firstProgram():  ## method name or test case name should start with test_***
  msg = "Hello"
  assert msg = "Hi", "Test failed because strings do not match"
def test_SecondProgram():
  a = 4
  b = 6
  assert a+2 == 6, "Addition do not match"
def test_SecondPCreditCard():
  a = 4
  b = 6
  assert a+2 == 6, "Addition do not match"

To run all python tests in a folder, py.test -v -s
to run a particualr test file,  py.test test_demo2.py -v -s
to filter based on test case names, py.test -k CreditCard -v -s

we can group test cases based on annotations.


@pytest.mark.smoke
def test_firstProgram():
  msg = "Hello"

### in another file:
@pytest.mark.smoke
def test_thirdProgram():
  msg = "Hello morning"
py.test -m smoke -v -s

to skip few tests and run remaining.

@pytest.mark.smoke
@pytest.mark.skip
def test_firstProgram():
  msg = "Hello"

## this particular test will be skipped.
## py.test -v -s
if  we want to make a test to run but skip the reporting of that, so the test will run and the errors won't stop remaining tests.


@pytest.mark.xfail
def test_firstProgram():
  msg = "Hello"