Skip to content

Commit

Permalink
Add unit testing to github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink authored Sep 13, 2024
1 parent 363d904 commit 0010bc8
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: testing
on: [push, pull_request]
jobs:
run:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.8, 3.9, "3.10", "3.11"]
steps:
- uses: actions/checkout@master

- name: Setup Python
uses: actions/setup-python@master
with:
python-version: ${{ matrix.python-version }}

- name: Run unit tests
run: |
pip install ".[test]"
pytest
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# required if you want to run document/test
# pip install 'mkdocs-macros-plugin[test]'
TEST_REQUIRE = ['mkdocs-macros-test', 'mkdocs-material>=6.2',
'mkdocs-include-markdown-plugin']
'mkdocs-include-markdown-plugin','click','pytest']

# --------------------
# Setup
Expand Down
Empty file added tests/conftest.py
Empty file.
100 changes: 100 additions & 0 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import pytest
import os
import shutil
import logging
from click.testing import CliRunner
from mkdocs.__main__ import build_command


def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
"""
Sets up a clean mkdocs directory
outputpath/testproject
├── docs/
└── mkdocs.yml
Args:
mkdocs_yml_path (Path): Path of mkdocs.yml file to use
output_path (Path): Path of folder in which to create mkdocs project
Returns:
testproject_path (Path): Path to test project
"""

testproject_path = output_path / "testproject"

# Create empty 'testproject' folder
if os.path.exists(testproject_path):
logging.warning(
"""This command does not work on windows.
Refactor your test to use setup_clean_mkdocs_folder() only once"""
)
shutil.rmtree(testproject_path)

# Copy correct mkdocs.yml file and our test 'docs/'
shutil.copytree(
os.path.join(os.path.dirname(mkdocs_yml_path), "docs"),
testproject_path / "docs",
)
if os.path.exists(os.path.join(os.path.dirname(mkdocs_yml_path), "assets")):
shutil.copytree(
os.path.join(os.path.dirname(mkdocs_yml_path), "assets"),
testproject_path / "assets",
)
shutil.copyfile(mkdocs_yml_path, testproject_path / "mkdocs.yml")

return testproject_path


def build_docs_setup(testproject_path):
"""
Runs the `mkdocs build` command
Args:
testproject_path (Path): Path to test project
Returns:
command: Object with results of command
"""

cwd = os.getcwd()
os.chdir(testproject_path)

try:
run = CliRunner().invoke(build_command)
os.chdir(cwd)
return run
except:
os.chdir(cwd)
raise



SHOULD_SUCCEED = [
"test/debug/mkdocs.yml",
"test/new_syntax/mkdocs.yml",
"test/no_module/mkdocs.yml",
"test/opt-in/mkdocs.yml",
"test/opt-out/mkdocs.yml",
"test/simple/mkdocs.yml",
]

SHOULD_FAIL = [
"test/module/mkdocs.yml",
"test/module_dir/mkdocs.yml",
]



@pytest.mark.parametrize("mkdocs_yml_path", SHOULD_SUCCEED)
def test_builds_succeeding(mkdocs_yml_path, tmp_path):
testproject_path = setup_clean_mkdocs_folder(mkdocs_yml_path, tmp_path)
result = build_docs_setup(testproject_path)
assert result.exit_code == 0, "'mkdocs build' did not succeed"

@pytest.mark.parametrize("mkdocs_yml_path", SHOULD_FAIL)
def test_builds_failing(mkdocs_yml_path, tmp_path):
testproject_path = setup_clean_mkdocs_folder(mkdocs_yml_path, tmp_path)
result = build_docs_setup(testproject_path)
assert result.exit_code != 0, "'mkdocs build' did not fail"

0 comments on commit 0010bc8

Please sign in to comment.