devopshq / artifactory

dohq-artifactory: a Python client for Artifactory
https://devopshq.github.io/artifactory/
MIT License
269 stars 137 forks source link

Mock ArtifactoryPath for local tests #406

Closed mdhaisne closed 1 year ago

mdhaisne commented 1 year ago

hello everyone ! In my project, I'd like to write unit tests of my class ArtifactoryDriver with has ArtifactoryPath as an instance attribute like below:


class ArtifactoryAgent:
    def __init__(self, path:str) -> None:
        self.root = ArtifactoryPath(path)

        if not self.root.exists():
            raise ArtifactoryException(f"Url `{url}` does not exist")

For my unit test I want to mock ArtifactoryPath with unittest so it uses local assets and I don't have to deploy an artifactory repository for test purposes. Is there an easy way to do that ?

Regards and thanks for your help !

beliaev-maksim commented 1 year ago

I would go for https://github.com/getsentry/responses

once PR https://github.com/getsentry/responses/pull/624 is merged, you will be able to record all the HTTP calls and their reponses

mdhaisne commented 1 year ago

I would go for https://github.com/getsentry/responses

once PR getsentry/responses#624 is merged, you will be able to record all the HTTP calls and their reponses

Thank for your reply. It does not seem to answer my need as I understand it. I don't want to record HTTP traffic. I'd like to avoid the need of an artifactory instance / server and have everything local. e.g. ArtifactoryPath("my_local_assets/"). But maybe I've misunderstood your point

beliaev-maksim commented 1 year ago

but if you just want to see that your code does what it supposed to do, then comparing HTTP traffic is enough, no?

mdhaisne commented 1 year ago

but if you just want to see that your code does what it supposed to do, then comparing HTTP traffic is enough, no?

It means that I need to deploy an artifactory instance ? (So that there is a traffic)

Quick and dirty example:

# File structure
artifactory_agent.py
artifactory_agent_test.py
assets/
   foo_1.txt
   foo_2.txt
   foo_3.txt
# unit test
def test_get_foos():
    agent  = ArtifactoryAgent("assets")
    foos = agent.get_foos()
    assert len(foos) == 3

the constraint is that ArtifactoryPath should use local assets this why I'm looking for mocking mechanisms at first sights. But maybe there is an easier way to perform such tests. I'm kind of new in this repo.

beliaev-maksim commented 1 year ago

this library sends HTTP requests. If you want to test, that right requests were executed in right order, then mock them

allburov commented 1 year ago

Hi! I'd go with using monkeypatch and mocks for that https://docs.pytest.org/en/7.1.x/how-to/monkeypatch.html If you don't use specific method - it's fine to believe that the library works and only what you need to test is the script's logic.

IMO, recording http requests and responses are useful for testing THIS library, but not the script with logic.

beliaev-maksim commented 1 year ago

@allburov Here are pros and cons With http you can easily write tests in a matter of minutes

With monkeypatch you really have to invest time to write tests. However, as the general approach it will produce more explicit results and indeed, user should rely and trust our internal logic. Because for our reasons we may change internal http code, but keep API consistent

mdhaisne commented 1 year ago

Thanks for your help, I think I got enough feedbacks ! I'm closing the issue.