This is my conftest.py
:
class ValueStorage:
ime_rute = None
web_driver = None
@pytest.fixture(params=["chrome"], scope="class")
def init_driver(request):
start = datetime.now()
tester = os.environ["USERNAME"]
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.addHandler(logging.FileHandler(filename=f'.\\Reports\\{date.today()}_duration.log'))
if request.param == "chrome":
options = ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("--no-sandbox")
if Options.HEADLESS == 1:
options.add_argument('--headless')
options.add_experimental_option("excludeSwitches", ["enable-logging"])
ValueStorage.web_driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
if request.param == "firefox":
ValueStorage.web_driver = webdriver.Firefox(GeckoDriverManager().install())
request.cls.driver = ValueStorage.web_driver
yield
end = datetime.now()
logger.info(
f"{end}: --- DURATION: {end - start} --- ROUTE NAME: {ValueStorage.ime_rute} --- HEADLESS: {Options.HEADLESS} --- RUN BY: {tester}")
ValueStorage.web_driver.close()
I'm running the tests from the command line using
pytest -k example -vv
(where "example" is the test name).
I wish to repeat the test multiple times, so I'm using @pytest.mark.repeat(100)
decorator:
@pytest.mark.repeat(100)
@pytest.mark.develop
def test_example(self):
# test code
Output in the duration.log file will be like
2021-04-29 12:10:57.245345: --- DURATION: 0:00:24.612506 --- ROUTE NAME: 210429-3843-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:21.726882: --- DURATION: 0:00:24.364809 --- ROUTE NAME: 210429-3844-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:21.726882: --- DURATION: 0:00:24.364809 --- ROUTE NAME: 210429-3844-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:57.758704: --- DURATION: 0:00:35.897821 --- ROUTE NAME: 210429-3845-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:57.758704: --- DURATION: 0:00:35.897821 --- ROUTE NAME: 210429-3845-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:57.758704: --- DURATION: 0:00:35.897821 --- ROUTE NAME: 210429-3845-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
So, what is happening is that the logger.info()
line writes to log once for the first test run, twice for the second one, three times for the third and so on.
I tried changing scope of the pytest fixture to 'class', but then I'm getting that line logged only after the test has been run 100 times.
I want to get the logger.info()
write to log after each test execution once. What do I need to do?