-
Notifications
You must be signed in to change notification settings - Fork 221
Description
Discussed in #146
Originally posted by YousefAldabbas July 18, 2024
Hi all,
I'm planning to open an issue and close it. I want to know the community's/contributors' thoughts before proceeding.
The existing unit tests are relying on each other to pass the unit tests for example test_update_user
relies on test_post_user
success, Which violate unit tests best practices unit tests should be able to run in isolation src for example.
Also test_update_user
is calling another function which is relies on the server itself to generate token and then send it with the tests requests , it should be mocked.
issue: usage of _get_token & environment variables to create user for testing
after implementing applying
def test_update_user(db: Session, client: TestClient) -> None:
user = generators.create_users(db) #generate mocked user
override_dependancy(auth_dep, user)
updated_name = f"Updated {user.name}"
response = client.patch(
f"/api/v1/user/{user.username}",
json={"name": updated_name}
)
assert response.status_code == 200
# now we can validate that the data is updated!
# something like
# assert response.json()["name"] == updated_name
and override function code (copied from one of my repos)
def override_dependency(dependency: Callable[..., Any], mocked_response: Any):
"""
Overrides a dependency with a mocked response.
Parameters:
dependency (Callable[..., Any]): The dependency to override.
mocked_response (Any): The mocked response to use.
Returns:
None
Example:
`override_dependency(get_data, mocked_data)`
"""
app.dependency_overrides[dependency] = lambda: mocked_response
after that we can remove these env variable
test_name = settings.TEST_NAME
test_username = settings.TEST_USERNAME
test_email = settings.TEST_EMAIL
test_password = settings.TEST_PASSWORD
we can also generate username and password for the admin using faker package
theres two new packages i will be adding:
- psycopg2-binary to support db fixture (get_session)
- faker
in the end we will be able to tests all endpoints in isolation