Skip to content

Commit

Permalink
Merge pull request #73 from schireson/dc/tests-path
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin authored Feb 21, 2023
2 parents efcb4a4 + cc6076d commit c4920c9
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 23 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Github Release/Publish PyPi

on:
push:
tags:
- "v*.*.*"

jobs:
gh-release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Release
uses: softprops/action-gh-release@v1
with:
generate_release_notes: true

publish-pypi:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Run image
uses: abatilo/[email protected]
with:
poetry-version: 1.2.0

- name: Publish
env:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
run: |
poetry config pypi-token.pypi $PYPI_TOKEN
poetry publish --build
61 changes: 56 additions & 5 deletions docs/source/running.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
Running Tests
=============

:code:`Pytest Alembic` automatically adds a flag, :code:`pytest --test-alembic`, which will
automatically invoke the baked-in tests.
You have two primary options for running the configured set of tests:

1. Automatically at the command-line via ``--test-alembic``

:code:`Pytest Alembic` automatically adds a flag, :code:`pytest --test-alembic`, which will
automatically invoke the baked-in tests.

This can be convenient if you want to exclude migrations tests most of the time, but include
them for e.g. CI. By default, ``pytest tests`` would then, **not** run migrations tests.

Additionally, it means you don't need to manually include the tests in a test file somewhere
in your project.

If your tests dont generally reside at/below a ``tests/`` directory with a ``tests/conftest.py``
file, you can/should set the :code:`pytest_alembic_tests_path` option, described
below.

2. You can directly import the tests you want to include at any point in your project.

.. code-block:: python
:caption: tests/test_migrations.py
from pytest_alembic.tests import (
test_model_definitions_match_ddl,
test_single_head_revision,
test_up_down_consistency,
test_upgrade,
)
This can be convenient if you always want the migrations tests to run, or else want a reference
to the tests' existence somewhere in your source code. Pytest would automatically include
the tests every time you run i.e. :code:`pytest tests`.

In either case, you can exclude migrations tests using pytest's "marker" system, i.e.
``pytest -m "not alembic"``.




Configuration
-------------
Expand Down Expand Up @@ -34,9 +70,24 @@ behavior.

.. note::

As of pytest-alembic version 0.8.5, this option is ignored. Tests will be registered
at the top level, and `--test-alembic` will automatically include the tests regardless
of the provided path.
As of pytest-alembic version 0.8.5, this option is ignored. Instead, if you require customizing
the registration location, you should use :code:`pytest_alembic_tests_path` instead.

* :code:`pytest_alembic_tests_path`

.. note::

Introduced in v0.10.1.

The location at which the built-in tests will be bound. This defaults to 'tests/conftest.py'.
Typically, you would want this to coincide with the path at which your `alembic_engine` is being
defined/registered. Note that this path must be the full path, relative to the root location
at which pytest is being invoked.

This option has replaced :code:`pytest_alembic_tests_folder` due to changes in how pytest test collection
needed to be performed in around pytest ~7.0.

Additionally, this option is only required if you are using the :code:`--test-alembic` flag.


Alembic Config
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-alembic"
version = "0.10.0"
version = "0.10.1"
description = "A pytest plugin for verifying alembic migrations."
authors = [
"Dan Cardin <[email protected]>",
Expand Down
19 changes: 12 additions & 7 deletions src/pytest_alembic/plugin/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ def pytest_addoption(parser):
f"included. Valid options include: {experimental_tests}",
)
parser.addini(
"pytest_alembic_tests_folder",
"The location under which the built-in tests will be bound. This defaults to 'tests/' "
"(the tests themselves then being executed from tests/pytest_alembic/*), the typical test "
"location. However this can be customized if pytest is, for example, invoked from a parent "
"directory like `pytest folder/tests`, or the tests are otherwise located at a different "
"location, relative to `pytest`s invocation.",
default="tests",
"pytest_alembic_tests_path",
"The location at which the built-in tests will be bound. This defaults to 'tests/conftest.py'. "
"Typically, you would want this to coincide with the path at which your `alembic_engine` is being "
"defined/registered. Note that this path must be the full path, relative to the root location "
"at which pytest is being invoked.",
default="tests/conftest.py",
)

group = parser.getgroup("collect")
Expand All @@ -49,6 +48,12 @@ def pytest_addoption(parser):
help=f"List of built-in tests to exclude. Valid options include: {default_tests}",
dest="pytest_alembic_exclude",
)
group.addoption(
"--alembic-tests-path",
default="tests/conftest.py",
help="The location at which the built-in tests will be bound.",
dest="pytest_alembic_tests_path",
)


def pytest_configure(config):
Expand Down
18 changes: 11 additions & 7 deletions src/pytest_alembic/plugin/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from dataclasses import dataclass
from pathlib import Path
from pathlib import Path, PurePath
from typing import Callable, Dict, List, Optional

import pytest
Expand Down Expand Up @@ -31,12 +31,16 @@ def pytest_collect_file(self, path, parent): # type: ignore
return TestCollector.from_parent(parent, fspath=path)

def should_register(self, path):
if path.suffix != ".py":
return False

if not self.registered:
self.registered = True
return True
tests_path = PurePath(
self.config.option.pytest_alembic_tests_path
or self.config.getini("pytest_alembic_tests_path")
or "tests/conftest.py"
)
relative_path = path.relative_to(self.config.rootpath)
if relative_path == tests_path:
if not self.registered:
self.registered = True
return True

return False

Expand Down
13 changes: 10 additions & 3 deletions tests/plugin/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

from pytest_alembic.plugin.plugin import OptionResolver, parse_test_names

pytest_options = (
"--test-alembic",
"--alembic-tests-path",
"conftest.py",
"-vv",
)


def test_parse_raw_test_names_empty_skips():
result = sorted(parse_test_names("up_down_consistency,foo\n\n\nbar\n"))
Expand Down Expand Up @@ -88,7 +95,7 @@ def test_disabled_cli(self, testdir):
def test_include_cfg(self, testdir):
testdir.copy_example("test_no_data")
testdir.makefile(".ini", pytest="[pytest]\npytest_alembic_include=single_head_revision\n")
result = testdir.runpytest("--test-alembic", "-vv")
result = testdir.runpytest(*pytest_options)
stdout = result.stdout.str()
print(stdout)

Expand All @@ -98,7 +105,7 @@ def test_include_cfg(self, testdir):
def test_exclude_cfg(self, testdir):
testdir.copy_example("test_no_data")
testdir.makefile(".ini", pytest="[pytest]\npytest_alembic_exclude=single_head_revision\n")
result = testdir.runpytest("--test-alembic", "-vv")
result = testdir.runpytest(*pytest_options)
stdout = result.stdout.str()
print(stdout)

Expand All @@ -107,7 +114,7 @@ def test_exclude_cfg(self, testdir):

def test_included_tests_start_with_tests(self, testdir):
testdir.copy_example("test_no_data")
result = testdir.runpytest("--test-alembic", "-vv")
result = testdir.runpytest(*pytest_options)
stdout = result.stdout.str()
print(stdout)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
def run_pytest(pytester, *, success=True, passed=4, skipped=0, failed=0, test_alembic=True):
args = [
"--test-alembic",
"--alembic-tests-path",
"conftest.py",
"-vv",
"-s",
]
Expand Down

0 comments on commit c4920c9

Please sign in to comment.