Coverage for gws-app/gws/test/__init__.py: 100%

0 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""GWS Test Framework 

2 

3To run tests in GWS, you need to start a docker-compose environment with the GWS container and some auxiliary containers, and run the test script in the GWS container. 

4 

5All test functionality is invoked via ``make.sh test`` commands. 

6 

7There are two ways to run the tests: 

8 

9- automatically, with ``make.sh test go``. This configures the docker-compose environment for testing, runs tests, and shuts down. 

10- manually, where you first start the environment with ``make.sh test start`` and then invoke tests with ``make.sh test run``. 

11 

12In the latter case, it's convenient to start the compose environment in the foreground and invoke ``test run`` in another shell. 

13 

14When testing manually, you can also select specific tests with the ``--only`` option and provide additional pytest options. See ``make.sh test -h`` for details. 

15 

16User/group 

17---------- 

18 

19On Linux systems, the user running the tests (you) must be able to run ``docker``. Use ``sudo usermod -aG docker $USER`` to add yourself to the ``docker`` group. 

20See https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user 

21 

22By default, test containers use your uid/gid to run processes (like the postgres server), you can override this with ``runner.uid`` and ``runner.gid`` options. 

23 

24 

25Configuration 

26------------- 

27 

28On your machine, you need a dedicated directory where the tester stores its stuff ("base_dir"). 

29 

30The configuration is in the file "test.ini" in the application root directory. If you need custom options (e.g., a custom base directory path), create a secondary "ini" file with your overrides and pass it as ``make.sh test --ini myconfig.ini``. 

31 

32Writing tests 

33------------- 

34 

35All test files must end with ``_test.py``. All test functions must start with ``test_``. It is recommended to always import the test utilities library, which provides some useful shortcuts and mocks. Here is an example of a test file:: 

36 

37 '''Testing the foo package.''' 

38 

39 import gws 

40 import gws.lib.foo as foo 

41 import gws.test.util as u 

42 

43 def test_one(): 

44 assert foo.bar == 1 

45 

46 def test_two(): 

47 with u.raises(ValueError): 

48 foo.blah() 

49 

50Coverage reports 

51---------------- 

52 

53Pass the ``--coverage`` option to ``test run`` to create a coverage report. It will be created in ``base_dir/coverage``. 

54With ``test go``, the coverage report is created automatically. 

55 

56"""