What I’m trying to do:
I’m trying to run unit tests locally for server modules.
What I’ve tried and what’s not working:
I’ve got python3 -m anvil.run_app_via_uplink YourPackageName running, but it keeps erroring when trying to import from individual server_code files (packages are fine) with the ModuleNotFoundError: No module named 'Education_App.DBAccess' error.
Is there any documentation for this command and what its useful for and/or how to set it up properly? How do I need to change my file structures and/or imports to make them work both while running in the IDE and locally?
So it turns out I don’t actually need to do that, all I needed to do was run the python script normally with the python3 -m unittest command as long as I ran the anvil.server.connect command before defining my tests.
Here is the start of my file that made everything work with an uplink script that I just run locally without connecting to my online app:
"""Tests for Server Modules."""
import anvil.server
import sys
import unittest
sys.path.append("..") # This is so I can import from server_code and client_code from my unit_tests directory
anvil.server.connect("<your uplink key>")
from server_code.ModuleName import some_function_or_object
class Tests(unittest.TestCase):
def test_function(self):
"""Runs some test"""
Also, if you are doing any imports in your server code from modules, you do have to add some conditional imports like so to make it work:
if "App_Name" in __name__:
from ..Module import something
else:
from client_code.Module import something
In the end, that was the only import I had to change – If there’s any advice for what I can do structurally to avoid that, that would be much appreciated.