Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update integration tests to use pytest conventions #743

Closed
Sherwin-14 opened this issue Jul 2, 2024 · 12 comments · Fixed by #808
Closed

Update integration tests to use pytest conventions #743

Sherwin-14 opened this issue Jul 2, 2024 · 12 comments · Fixed by #808
Labels
enhancement New feature or request

Comments

@Sherwin-14
Copy link
Contributor

Sherwin-14 commented Jul 2, 2024

Integration tests have been failing silently for some time (#738). In the course of adding new integration tests (#736), we found some errors that were not reporting very much information, presumably either because the code that's generating the errors are not following pytest conventions.

  1. Tests which import assertions and call for example assertions.assertTrue(x) should be updated to builtin assertions, for example assert x is True.
  2. Tests which do setup at the module level should be updated to use fixtures
@mfisher87 mfisher87 added the bug Something isn't working label Jul 2, 2024
@mfisher87 mfisher87 changed the title Refactor some unit tests Update integration tests to use pytest conventions Jul 2, 2024
@mfisher87 mfisher87 removed the bug Something isn't working label Jul 2, 2024
@Sherwin-14
Copy link
Contributor Author

Sherwin-14 commented Jul 6, 2024

Hey, Could you assign me this one? I have started to work on this and I had some doubts. First of all, I have updated all the tests having assertions.assertTrue(x) to assert x is true. So this is exactly what we want here isn't it? Secondly I went through all the test files and wasn't able to find the module level setups without fixtures. Can you highlight some specific ones so that I get a hang of what I need to do here?

@mfisher87
Copy link
Collaborator

mfisher87 commented Jul 6, 2024

This test file is a good example:

assertions = unittest.TestCase("__init__")
# we need to use a valid EDL credential
assertions.assertTrue("EARTHDATA_USERNAME" in os.environ)
assertions.assertTrue("EARTHDATA_PASSWORD" in os.environ)
auth = Auth().login(strategy="environment")
assertions.assertTrue(auth.authenticated)
logger.info(f"Current username: {os.environ['EARTHDATA_USERNAME']}")
logger.info(f"earthaccess version: {earthaccess.__version__}")
store = Store(auth)
Looks like most of the integration test files have this problem, but not all (e.g. test_auth.py looks OK).

@Sherwin-14
Copy link
Contributor Author

This test file is a good example: https://github.com/nsidc/earthaccess/blob/main/tests/integration/test_onprem_download.py#L51-L63 Looks like most of the integration test files have this problem, but not all (e.g. test_auth.py looks OK).

Yeah but I guess the granules function here is used as a setup and it has already been fixturized(If that's a term). Also this means that we juts need to update codeblocks like the above example, and not to tinker with the other assertions.assertTrue(x) expressions.

@mfisher87
Copy link
Collaborator

I wouldn't describe get_sample_granules or the auth store setup as "fixturized" at the moment, at least under Pytest conventions, since they're not using a fixture decorator (and the auth stuff is bare at the module level). Have you checked out this doc yet? https://docs.pytest.org/en/6.2.x/fixture.html

The pytest fixture decorators provide advanced functionality for managing the fixture's lifecycle or scope. That way, we don't need to repeat behavior unnecessarily (we do this same credential-reading stuff at the module level in several files in addition to test_onprem_download.py, but we only need to do it once:

assertions = unittest.TestCase("__init__")
# we need to use a valid EDL credential
assertions.assertTrue("EARTHDATA_USERNAME" in os.environ)
assertions.assertTrue("EARTHDATA_PASSWORD" in os.environ)
auth = Auth().login(strategy="environment")
assertions.assertTrue(auth.authenticated)
logger.info(f"Current username: {os.environ['EARTHDATA_USERNAME']}")
logger.info(f"earthaccess version: {earthaccess.__version__}")
store = Store(auth)
), and we get better reporting from pytest to help us understand what's going on :)

@Sherwin-14
Copy link
Contributor Author

Oh there seems to be some misunderstanding here! I was talking about the granules function in test_kerchunk.py that is fixturized at the moment. So I was basically asking for one more example of a setup process that is not fixturized uptill now.

@mfisher87
Copy link
Collaborator

Gotcha, thanks for clarifying! Just double-checking, do you have the info you need now?

@Sherwin-14
Copy link
Contributor Author

Sherwin-14 commented Jul 6, 2024

Not yet 😅. I just need an example of the setup function that is not fixturized yet.

@mfisher87
Copy link
Collaborator

In this case, it's not a function, it's the module-level code I linked above which creates an Auth and Store object. Does that make sense?

@mfisher87 mfisher87 added the enhancement New feature or request label Jul 6, 2024
@Sherwin-14
Copy link
Contributor Author

The auth and store objects can be considered as setup's, is that what you are implying? Also I think that the conftest.py file should contain all the fixturized functions. So I am thinking of putting all the fixturized functions in that file, would that be fine with you?

@mfisher87
Copy link
Collaborator

The auth and store objects can be considered as setup's, is that what you are implying?

Correct!

Also I think that the conftest.py file should contain all the fixturized functions. So I am thinking of putting all the fixturized functions in that file, would that be fine with you?

Sounds great :) Thanks again @Sherwin-14 for your work!

@Sherwin-14
Copy link
Contributor Author

Hey, In that case may be grouping them into a function and then fixturizing it can be a solution isn't it? or Am I missing something?

@mfisher87
Copy link
Collaborator

mfisher87 commented Jul 7, 2024

Yes, exactly :) I don't think you're missing anything! I think we want scope="module" for the new fixture.

chuckwondo added a commit to chuckwondo/earthaccess that referenced this issue Sep 16, 2024
chuckwondo added a commit that referenced this issue Sep 16, 2024
Also, support `NETRC` environment variable for specifying
alternative location of `.netrc` file.

Fixes #806
Fixes #743
Fixes #480
chuckwondo added a commit to chuckwondo/earthaccess that referenced this issue Sep 17, 2024
chuckwondo added a commit to chuckwondo/earthaccess that referenced this issue Sep 22, 2024
chuckwondo added a commit to chuckwondo/earthaccess that referenced this issue Sep 24, 2024
chuckwondo added a commit to chuckwondo/earthaccess that referenced this issue Sep 26, 2024
@github-project-automation github-project-automation bot moved this from 🆕 New to ✅ Done in earthaccess project Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
2 participants