Running unit tests locally with Pytest

What I’m trying to do:

Run unit tests on modules in both the client_code and server_code locally with pytest. This is necessary so that the app can be automatically tested with CI.

I have a Form in the client code called Main. I want to import the Main class in the pytest unit test so I can test its methods.

What I’ve tried and what’s not working:

I have the beginning of a test, but it fails at the first line.


def test_main():
    from client_code.Main import Main

    main = Main()
    assert main is not None

When I run the test with python -m pytest, the test fails with this error on the import line:

    def test_main():
>       from client_code.Main import Main

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

>   from ._anvil_designer import MainTemplate
E   ModuleNotFoundError: No module named 'client_code.Main._anvil_designer'

client_code/Main/ ModuleNotFoundError

Possible reason/solution:

I suspect this fails because the ._anvil_designer module does not exist unless the anvil_app_server is running. The MainTemplate module will also not exist unless the app server has interpreted the form_template.yaml and converted it into an importable module.

Is there are way to run the app server from within Python so that I can have all the anvil Form modules available for testing in this manner?

Assume all the appropriate packages are installed and available (ie, I can run the app locally and it works as expected in localhost).

1 Like

The ._anvil_designer module is generated at runtime in the browser to produce the FormTemplate class from the yaml.

You may want to check out @owen.campbell test library that may do what you need.

It doesn’t seem like it’s what you’re after, but you could also look at using the client side unittest module. I’m not sure how many have tried doing that. I haven’t seen many questions about using it so I assume not. But I’ve used it a fair bit so can point in the right direction if you go down that route.