diff --git a/.gitignore b/.gitignore index 5fbd4ea0feb747e5026f2e03709393cadf0045db..18f8c65f4b93c71256d19e9f97026f399ce7c644 100644 --- a/.gitignore +++ b/.gitignore @@ -90,4 +90,12 @@ build/ data-dev # webpack analyze data -stats.json \ No newline at end of file +stats.json + +# JetBrains IDE folders +.idea/ + +# pytest coverage reports +.coverage +cov.xml +cov.html/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 00a36093c8acd358c7f35e9199689550973372e2..c31378220478a46cef22952a3ba48748811d3ecd 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,3 @@ - # This base image can be found in 'Dockerfile' image: zesje/base @@ -36,11 +35,6 @@ test_js: stage: test script: yarn test:js -test_py: - <<: *python_packages - stage: test - script: yarn test:py - lint_js: <<: *node_modules stage: test @@ -54,3 +48,13 @@ lint_py: allow_failure: true script: - yarn lint:py + +test_py: + <<: *python_packages + stage: test + script: + - yarn test:py:cov + artifacts: + paths: + - cov.html/ + expire_in: 1 week diff --git a/README.md b/README.md index 1155b27641ad15059efc00aa53c26cbfa4241072..47181611e5f7205d619462e0d67273e0d94e54f9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[](https://gitlab.kwant-project.org/zesje/zesje/commits/master) + # Welcome to Zesje Zesje is an online grading system for written exams. @@ -63,6 +65,33 @@ or `zesje/`. You can run the tests by running yarn test + +#### Viewing test coverage + +As a test coverage tool for Python tests, `pytest-cov` is used. + +To view test coverage, run + + yarn test:py:cov + +A coverage report is now generated in the terminal, as an XML file, and in HTML format. +The HTML file shows an overview of untested code in red. + +##### Viewing coverage in Visual Studio Code + +There is a plugin called Coverage Gutter that will highlight which lines of code are covered. +Simply install Coverage Gutter, after which a watch button appears in the colored box at the bottom of your IDE. +When you click watch, green and red lines appear next to the line numbers indicating if the code is covered. + +Coverage Gutter uses the XML which is produced by `yarn test:py:cov`, called `cov.xml`. This file should be located in the main folder. + +##### Viewing coverage in PyCharm +To view test coverage in PyCharm, run `yarn test:py:cov` to generate the coverage report XML file `cov.xml` if it is not present already. + +Next, open up PyCharm and in the top bar go to **Run -> Show Code Coverage Data** (Ctrl + Alt + F6). + +Press **+** and add the file `cov.xml` that is in the main project directory. +A code coverage report should now appear in the side bar on the right. #### Policy errors diff --git a/package.json b/package.json index 793cefcd9ec01c41e9f2ca5e2829ef562691f397..732b0977b67167a471bf54ec7a39c1a618e52f6f 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "main": "index.js", "license": "AGPL-3.0", "scripts": { - "dev": "concurrently --kill-others --names \"WEBPACK,PYTHON,CELERY\" --prefix-colors \"bgBlue.bold,bgGreen.bold,bgRed.bold\" \"webpack-dev-server --hot --inline --progress --config webpack.dev.js\" \"ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg python3 zesje\" \"ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg celery -A zesje.celery worker -l info --autoscale=4,1 --max-tasks-per-child=16 \"", + "dev": "concurrently --kill-others --names \"WEBPACK,PYTHON,CELERY\" --prefix-colors \"bgBlue.bold,bgGreen.bold,bgRed.bold\" \"webpack-dev-server --hot --inline --progress --config webpack.dev.js\" \"ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg python3 zesje\" \"ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg celery -A zesje.celery worker\"", "build": "webpack --config webpack.prod.js", "ci": "yarn lint; yarn test", "lint": "yarn lint:js; yarn lint:py", @@ -16,7 +16,9 @@ "analyze": "webpack --config webpack.prod.js --profile --json > stats.json; webpack-bundle-analyzer stats.json zesje/static", "migrate:dev": "ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg FLASK_APP=zesje/__init__.py flask db upgrade", "migrate": "FLASK_APP=zesje/__init__.py flask db upgrade", - "prepare-migration": "ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg FLASK_APP=zesje/__init__.py flask db migrate" + "prepare-migration": "ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg FLASK_APP=zesje/__init__.py flask db migrate", + "test:py:cov": "python3 -m pytest -v -W error::RuntimeWarning --cov=zesje --cov-report=xml:cov.xml --cov-report=html:cov.html --cov-report=term tests/", + "migrate-down": "FLASK_APP=zesje/__init__.py flask db downgrade" }, "standard": { "parser": "babel-eslint", diff --git a/requirements-dev.txt b/requirements-dev.txt index 729f564bb1f31c49a88e5ae1b4bb8540e47abd8d..7256878a85e4ee553f1382d32a6ef427133c04b4 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,6 +1,7 @@ # Tests pytest pyssim +pytest-cov # Linting flake8