Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactor/Enhance Tests Setup #146

arab0v started this conversation in Ideas
Discussion options

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

image

_get_token code
image

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

You must be logged in to vote

Replies: 1 comment

Comment options

This would be great, @YousefAldabbas! You're more than welcome to tackle it

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /