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.
Test:
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
tests/test_client_code.py:2:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
> from ._anvil_designer import MainTemplate
E ModuleNotFoundError: No module named 'client_code.Main._anvil_designer'
client_code/Main/__init__.py:1: 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).