Coverage for website/tests/test_smoke_imports.py: 93%

27 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2025-09-13 15:29 -0300

1import importlib 

2import os 

3 

4 

5def test_import_website_modules(): 

6 root = os.path.dirname(os.path.dirname(__file__)) 

7 imported = 0 

8 errors = {} 

9 for dirpath, _dirnames, filenames in os.walk(root): 

10 # skip tests and migrations folders 

11 if "tests" in dirpath.split(os.sep) or "migrations" in dirpath.split(os.sep): 

12 continue 

13 for fn in filenames: 

14 if not fn.endswith(".py"): 

15 continue 

16 if fn == "__init__.py": 

17 # import package 

18 mod_path = os.path.relpath(dirpath, root).replace(os.sep, ".") 

19 if mod_path == ".": 

20 module_name = "website" 

21 else: 

22 module_name = f"website.{mod_path}" 

23 else: 

24 mod_rel = os.path.relpath(os.path.join(dirpath, fn), root)[:-3] 

25 module_name = "website." + mod_rel.replace(os.sep, ".") 

26 

27 # skip obvious heavy or test-only files 

28 if any( 

29 x in module_name for x in (".tests", ".migrations", "tests.", "test_") 

30 ): 

31 continue 

32 

33 try: 

34 importlib.import_module(module_name) 

35 imported += 1 

36 except Exception as e: 

37 errors[module_name] = str(e) 

38 

39 # sanity check: we should have imported at least some modules 

40 assert ( 

41 imported > 10 

42 ), f"Imported too few modules: {imported}; errors: {list(errors.keys())[:5]}"