Skip to content

Comments

Move old API test code into pytest framework#327

Merged
sveinse merged 5 commits intocustom-components:masterfrom
steinmn:move_api_test_code
Sep 28, 2025
Merged

Move old API test code into pytest framework#327
sveinse merged 5 commits intocustom-components:masterfrom
steinmn:move_api_test_code

Conversation

@steinmn
Copy link
Contributor

@steinmn steinmn commented Sep 24, 2025

but disable it on Github actions since it requires login credentials.

but disable it on Github actions since it requires login credentials.

# Set manually, or when running 'scripts/test --skip-api'
SKIP_API_TEST = os.getenv("SKIP_ZAPTEC_API_TEST") == "true"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to move them to conftest.py and make a proper mark. This is probably useful to several test function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please clarify what you mean by "proper mark"? I'm probably too tired right now, but I just couldn't get fixtures and mark to behave like I wanted them to. Will take another look at it over the weekend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good nights sleep and some sparring with ChatGPT worked wonders, please have a look at the updated version

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that solution is ok. As you've probably seen already, I posted a proposal to how to do it by the use of additional command line parameters. You can chose which to use. In pytest there isn't a right way and a wrong way, just alternative ways to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering how limited our testing still is, I think I prefer to have the API-login-dependent tests run by default and then make exceptions where they don't run. Once we have higher test coverage and the ability to mock out stuff requiring login, we can reconsider to only run these full tests when adding additional arguments.

@sveinse
Copy link
Collaborator

sveinse commented Sep 28, 2025

This is how mark can be customized:

# conftest.py
def pytest_addoption(parser):
    parser.addoption(
        "--additional", action="store_true", default=False, help="Run additional Zaptec tests"
    )

def pytest_collection_modifyitems(config, items):
    skip_additional = None
    if not config.getoption("--additional"):
        skip_additional = pytest.mark.skip(reason="Skip test when --additional isn't used")
    for item in items:
        if skip_additional is not None and "additional" in item.keywords:
            item.add_marker(skip_additional)

This can now be used with mark in tests:

@pytest.mark.additional
def test_some_function():
    ....

This results in a new command line option --additional which runs the test functions that have been marked. Otherwise it should not. Note I haven't tested the above code, so it might not work, but this is the gist of it. https://docs.pytest.org/en/stable/how-to/writing_hook_functions.html

@steinmn steinmn requested a review from sveinse September 28, 2025 15:21
Copy link
Collaborator

@sveinse sveinse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any more objections. Approved.

@sveinse sveinse merged commit 89a5cc5 into custom-components:master Sep 28, 2025
6 checks passed
@sveinse sveinse added this to the v0.8.3 milestone Sep 28, 2025
@steinmn steinmn deleted the move_api_test_code branch September 28, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants