Skip to content

Commit

Permalink
tests: setup test framework
Browse files Browse the repository at this point in the history
The tests are only sample tests to get things started.
  • Loading branch information
robvdl committed Mar 6, 2024
1 parent 628edf1 commit 3ac43a0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
*.pyc
*~
.coverage
coverage.xml
coverage
reports/
build/
dist/
venv/
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ Then also install gunicorn and run:
pip install gunicorn
gunicorn sambal:app -w 4

Running Tests
-------------

To run the tests first make sure the optional dependency group called `test`
is installed:

pip install -e .[test]

Then just run pytest:

pytest

Coverage reports end up in the `reports` folder.

Why Pyramid
-----------

Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ dependencies = [
"WTForms==3.1.2",
]

[project.optional-dependencies]
test = [
"pytest==8.0.2",
"pytest-cov==4.1.0",
"WebTest==3.0.0",
]

[project.license]
text = "GPL-3.0-only"

Expand All @@ -53,3 +60,7 @@ license-files = [

[tool.distutils.bdist_wheel]
universal = true

[tool.pytest.ini_options]
python_files = ["tests.py", "test_*.py", "*_tests.py"]
addopts = "--cov=sambal --cov-branch --cov-report=term --cov-report=html:reports/htmlcov --cov-report=xml:reports/coverage.xml --junitxml=reports/junit.xml"
34 changes: 34 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
import webtest
from pyramid.scripting import prepare


@pytest.fixture(scope="session")
def app():
"""Fixtures that returns the Sambal WSGI app.
We can inject env vars here if necessary, but only before the import.
"""
import sambal

return sambal.app


@pytest.fixture
def testapp(app):
"""Fixture that returns a Sambal WebTest app."""
return webtest.TestApp(
app,
extra_environ={
"HTTP_HOST": "example.com",
},
)


@pytest.fixture
def app_request(app):
"""Returns a real HTTP request for use with testing Sambal views."""
with prepare(registry=app.registry) as env:
req = env["request"]
req.host = "example.com"
yield req
8 changes: 8 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def test_root(testapp):
response = testapp.get("/", status=200)
assert b"Sambal Login" in response.body


def test_notfound(testapp):
response = testapp.get("/does-not-exist/", status=404)
assert response.status_code == 404
15 changes: 15 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from unittest.mock import Mock

from sambal.views.auth import login


def test_login_view(app_request):
"""A sample test that calls the login view directly.
The 'real request' lacks a matched_route property required by the
login view, which can be done in the test rather than fixture.
"""
app_request.matched_route = Mock()
context = login(app_request)
assert "form" in context
assert "return_url" in context

0 comments on commit 3ac43a0

Please sign in to comment.