pytest

Test code and branch coverage simultanously with Pytest

Test code and branch coverage simultanously with Pytest Question: I am using pytest to test my Python code. To test for code coverage (C0 coverage) I run pytest –cov and I can specify my desired coverage in my pyproject.toml file like this: [tool.coverage.report] fail_under = 95 I get this result with a coverage a 96.30%: …

Total answers: 1

Expect value of a dropdown select based on option text

Expect value of a dropdown select based on option text Question: We have a dropdown select <select id="favorite-colors" multiple> <option value="R">Red</option> <option value="G">Green</option> <option value="B">Blue</option> </select> In playwright we can use to check the value expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")]) See: https://playwright.dev/python/docs/api/class-locatorassertions#locator-assertions-to-have-values But how can I use the expect to check if the selection is "Red" or "Green" …

Total answers: 2

Pytest test fails for all cases when only one fails

Pytest test fails for all cases when only one fails Question: I have a Pytest file structured as below: @pytest.fixture(scope="class") def driver_init(request): driver = webdriver.Chrome("path tp driver") driver.maximize_window() request.cls.driver = driver driver.get(url) # code that navigates to a page that I need to validate data on yield driver.close() pytest.mark.usefixtures("driver_init") @pytest.mark.parametrize("user", get_users()) class TestValidate: @pytest.mark.order(1) def …

Total answers: 1

In Pytest, Is there a way to "mock" or prevent something from happening while collecting test?

In Pytest, Is there a way to "mock" or prevent something from happening while collecting test? Question: So assume the allowing : # services/foo.py from apps.bar import BarClass static_foos = BarClass().get_statics() class FooService() : …. and test code looking like : # tests/test_foo.py from services.foo import FooService def test_foo_something() : assert FooService.something() == True So …

Total answers: 2

pytest mark parametrize a fixture

pytest mark parametrize a fixture Question: I want to test a function get_xyz(a, b, foo), where foo is a class. A foo_fixture is defined in a conftest.py like so: @pytest.fixture() def foo_fixture(): return Foo() The fixture simply returns a new instance of a class Foo. How would I parametrize this foo_fixture as an argument to …

Total answers: 1

Mocking datetime.now using pytest-mock

Mocking datetime.now using pytest-mock Question: I have a function func_to_mock in module module_to_mock.py. My unit test is located in test_func_to_mock.py I am trying to mock datetime.datetime.now, however, I am struggling. I get the error TypeError: cannot set ‘now’ attribute of immutable type ‘datetime.datetime’. What am I missing here? module_to_mock.py: # module_to_mock.py import datetime def func_to_mock() …

Total answers: 1

Is it possible to put common checks for tests separately in pytest?

Is it possible to put common checks for tests separately in pytest? Question: I have a lot of negative tests with step that checks there had no effect to my system. To make sure that, added time.sleep() and then checks the system. Example: import pytest import time class TestIncorrect: def test_incorrect_table(): print("Step 1:") print("Do something …

Total answers: 1

I want to turn off output in pytest console

I want to turn off output in pytest console Question: I want to turn off output in Pytest console, so i can see only my logs I tried pytest -x -s -tb=no but its not exactly what i want I want to get rid of this output ======================================================== test session starts ========================================================= platform win32 — …

Total answers: 1