test/ – Testing

Note

To run tests you need to install python dependencies with pip install -r requirements.txt and pug client with npm install pug.

  • run tests: pytest tests

Writing tests

Every new feature need to be tested. All tests are located in test/, and this have the same project structure than src/, but all names are preceded by test_.

At the beggining of every test, set in global variables: filepath of .pug source file to test and directory located in.

The basic process of each test involves next steps:

  • Create a .pug page inside project source directory that will be tested. This page must contains a mixin execution string and an include pointing to the mixins source code that will be tested (in the same folder). See pug_utils.create_pug_page().
  • Renderize created pug page and test HTML output.
  • If possible, test web endpoints (see http_utils below).

Fixtures

Pytest fixtures are inyected from test/conftest.py and located at test/_fixtures/.

pug_utils.py

pug_utils.create_pug_page(src, content, includes=[])

Creates a pug page with the content provided inserting includes to files in the same folder.

Parameters:
  • src (str) – Filepath of new page.
  • content (str) – Content of the file, without includes.
  • includes (list) – Files to include with pug’s include syntax (include ./<...>.pug) at the beggining of the file. As default [].
Returns:

src parameter value.

Return type:

str

pug_utils.extract_pug_mixin(src, mixin_name)

Extract a mixin code from a .pug file

Parameters:
  • src (str) – Source filepath.
  • mixin_name (str) – Mixin name to extract.
Returns:

The whole mixin, including definition and body.

Return type:

str

http_utils.py

class http_utils.Response(text, status_code, url)

Represents HTTP response objects.

Parameters:
  • text (str) – Data retrieved by the request.
  • status_code (int) – Status code of the response.
  • url (str) – Request url.
http_utils.get(url, timeout=10)

Performs a GET request giving an url as first parameter.

Parameters:
  • url (str) – Request url.
  • timeout (int, float, optional) – Request expiration time in seconds.
http_utils.assert_200(url)

Assert if requesting with GET an url this returns 200 status code.

Parameters:url (str) – Request url.
Returns:Response object if is asserted True.
Return type:http_utils.Response
http_utils.assert_403(url)

Assert if requesting with GET an url this returns 403 HTTP error (Forbidden request).

Parameters:url (str) – Request url.
Returns:Response object if is asserted True.
Return type:http_utils.Response

Global variables

Global tests variables are located at _fixtures/constests.py module.

constests.py

PUG_CLI_PATH

Filepath of pug client.

*_DIR

You can get every directory path inside src/ folder importing from conftest.py the uppercase name of a src folder following by _DIR. For example, filepath of src/code is the value of CODE_DIR.