1010
1111from app import create_app
1212from authentication .authentication import auth_with_jwt
13- from config import config
13+ from config import config as project_config
1414from data_providers .clients .mongodb .mongo_database_client import MongoDatabaseClient
1515from features .todo .repository .todo_repository import TodoRepository , get_todo_repository
1616from tests .integration .mock_authentication import mock_auth_with_jwt
@@ -28,7 +28,7 @@ def test_client():
2828
2929@pytest .fixture (autouse = True )
3030def disable_auth ():
31- config .AUTH_ENABLED = False
31+ project_config .AUTH_ENABLED = False
3232 os .environ ["AUTH_ENABLED" ] = "False"
3333
3434
@@ -76,10 +76,45 @@ async def request_body():
7676 return request
7777
7878
79- def pytest_addoption (parser ):
80- parser .addoption ("--integration" , action = "store_true" , help = "run integration tests" )
79+ def pytest_configure (config : pytest .Config ):
80+ """Add markers to be recognised by pytest."""
81+ has_unit_option = config .getoption ("unit" , default = False )
82+ has_integration_option = config .getoption ("integration" , default = False )
83+ marker_expr = config .getoption ("markexpr" , default = "" )
84+ if marker_expr != "" and (has_integration_option or has_unit_option ):
85+ pytest .exit ("Invalid options: Cannot use --markexpr with --unit or --integration options" , 4 )
8186
8287
83- def pytest_runtest_setup (item ):
84- if "integration" in item .keywords and not item .config .getoption ("integration" ):
85- pytest .skip ("need --integration option to run" )
88+ def pytest_addoption (parser ):
89+ """Add option to pytest parser for running unit/integration tests."""
90+ parser .addoption ("--unit" , action = "store_true" , default = False , help = "run unit tests" )
91+ parser .addoption ("--integration" , action = "store_true" , default = False , help = "run integration tests" )
92+
93+
94+ def pytest_collection_modifyitems (config : pytest .Config , items : list [pytest .Item ]):
95+ """Add markers to tests based on folder structure."""
96+ unit_tests_directory = config .rootpath / "src/tests/unit"
97+ integration_tests_directory = config .rootpath / "src/tests/integration"
98+ for item in items :
99+ if item .path .is_relative_to (unit_tests_directory ):
100+ item .add_marker ("unit" )
101+ if item .path .is_relative_to (integration_tests_directory ):
102+ item .add_marker ("integration" )
103+
104+
105+ def pytest_runtest_setup (item : pytest .Item ):
106+ """Skip tests based on options provided."""
107+ has_unit_option = item .config .getoption ("unit" , default = False )
108+ has_integration_option = item .config .getoption ("integration" , default = False )
109+ match (has_unit_option , has_integration_option ):
110+ case (False , True ):
111+ # skip unit tests
112+ if "unit" in item .keywords :
113+ pytest .skip ("unit tests are skipped when explicitly running integration tests" )
114+ case (True , False ):
115+ # skip integration tests
116+ if "integration" in item .keywords :
117+ pytest .skip ("integration tests are skipped when explicitly running unit tests" )
118+ case _:
119+ # run all tests
120+ return
0 commit comments