From 95c47c49e022c0a969d9ffeaebc6593133bdd436 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Thu, 26 Dec 2019 16:27:37 -0600 Subject: [PATCH] New fixture to test that distribution is pgwui pluggable --- .gitignore | 1 + Makefile | 9 ++++++ README.rst | 6 ++++ setup.cfg | 7 +++++ setup.py | 1 + src/pgwui_testing/testing.py | 14 +++++++++ tests/test_testing.py | 61 +++++++++++++++++++++++++++++++++--- tox.ini | 17 +++++++++- 8 files changed, 110 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a550952..bd5d3f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ __pycache__/ .cache/ .coverage +.coverage.* .tox/ build/ devel/ diff --git a/Makefile b/Makefile index ef832df..7078d69 100644 --- a/Makefile +++ b/Makefile @@ -23,3 +23,12 @@ PGWUI_NAME := testing include Makefile_pgwui.mk + + +# For check +export COV_CORE_SOURCE := pgwui_testing +export COV_CORE_CONFIG := .coveragerc +export COV_CORE_DATAFILE := .coverage.eager + +# For clean +COVERAGE_STUFF := $(COVERAGE_STUFF) .coverage.* diff --git a/README.rst b/README.rst index f97451d..6b9b7cc 100644 --- a/README.rst +++ b/README.rst @@ -21,6 +21,12 @@ .. #End Of Copyright Marker# +.. Bugs: (Due to fixture testing) + "make check" picks up tox tests + "make run_tests" leaves .coverage.eager.* files about + Probably we need to get rid of the .coverage files in both cases. + (removing .coverage fixes "make check" pickup of tox dirs) + PGWUI_Testing ============= diff --git a/setup.cfg b/setup.cfg index 24eff81..f290ea5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,10 @@ [bdist_wheel] # Work with both python 2 and 3. universal=1 + +[tool:pytest] +# Tell coverage to turn on and append results so that when we +# test we don't lose coverage due to warnings of previously having +# imported a module. Necessary for testing fixtures. +# See: https://pytest-cov.readthedocs.io/en/latest/plugins.html +addopts = --cov --cov-append diff --git a/setup.py b/setup.py index 8d3f306..d17903f 100644 --- a/setup.py +++ b/setup.py @@ -129,6 +129,7 @@ setup( # Run-time dependencies. install_requires=[ + 'setuptools', # for pkg_resources module 'pyramid', 'WebTest >= 1.3.1', # py3 compat 'pytest>=3.7.4', diff --git a/src/pgwui_testing/testing.py b/src/pgwui_testing/testing.py index d5763c1..fb567fa 100644 --- a/src/pgwui_testing/testing.py +++ b/src/pgwui_testing/testing.py @@ -19,6 +19,8 @@ # Karl O. Pinc +import pkg_resources + from pytest import ( fixture, ) @@ -42,3 +44,15 @@ def pyramid_request_config(): request = DummyRequest() yield setUp(request=request) tearDown() + + +@fixture +def pgwui_component_entry_point(): + '''Test that the supplied pgwui component is a pgui.components entry point + ''' + def run(component): + return (component in + [entry_point.resolve().__name__ for entry_point in + pkg_resources.iter_entry_points('pgwui.components')]) + + return run diff --git a/tests/test_testing.py b/tests/test_testing.py index 29f5783..13063bb 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -1,4 +1,4 @@ -# Copyright (C) 2018 The Meme Factory, Inc. http://www.meme.com/ +# Copyright (C) 2018, 2019 The Meme Factory, Inc. http://www.meme.com/ # This file is part of PGWUI_Testing. # @@ -30,15 +30,16 @@ from pyramid.threadlocal import ( # Activiate our pytest plugin -pytest_plugins = ("pgwui",) - +pytest_plugins = ("pgwui_testing", + "pytester") # Test fixtures + # pyramid_config def test_pyramid_config(pyramid_config): - '''A pyramid_config is a Configurator instance''' + # A pyramid_config is a Configurator instance result = pyramid_config assert isinstance(result, Configurator) @@ -46,7 +47,57 @@ def test_pyramid_config(pyramid_config): # pyramid_test_config def test_pyramid_request_config(pyramid_request_config): - '''Is a Configurator instance and has a non-None request''' + # Is a Configurator instance and has a non-None request result = pyramid_request_config assert isinstance(result, Configurator) assert get_current_request() is not None + + +def test_pgwui_component_entry_point(testdir): + '''Test the pgwui_component_entry_point fixture + ''' + testdir.makepyfile( + ''' + import pgwui_testing.testing as testing + import pytest + + + # pgwui_component_entry_point + + def test_pgwui_component_entry_point_there( + monkeypatch, pgwui_component_entry_point): + # True when the component is a pgwui.components entry point + test_name = 'pgwui_example' + + class MockEntryPoint(): + def __init__(self, name): + self.name = name + + def resolve(self): + class MockModule(): + def __init__(self, name): + self.__name__ = name + + return MockModule(self.name) + + monkeypatch.setattr( + testing.pkg_resources, 'iter_entry_points', + lambda *args: [MockEntryPoint(test_name)]) + + assert pgwui_component_entry_point(test_name) is True + + + def test_pgwui_component_entry_point_not_there( + monkeypatch, pgwui_component_entry_point): + # False when the component is not pgwui.components entry point + monkeypatch.setattr( + testing.pkg_resources, 'iter_entry_points', + lambda *args: []) + + assert pgwui_component_entry_point('foo') is False + ''' + ) + + result = testdir.runpytest() + + result.assert_outcomes(passed=2) diff --git a/tox.ini b/tox.ini index 3c38d58..3627108 100644 --- a/tox.ini +++ b/tox.ini @@ -15,12 +15,24 @@ deps = pytest-cov twine # coverage + +# Tell coverage to turn on and append results so that when we +# test we don't lose coverage due to warnings of previously having +# imported a module. Necessary for testing fixtures. +# (Also, in commands=, we don't: --cov=pgwui_testing) +# (And, we have the [tool:pytest] section.) +# See: https://pytest-cov.readthedocs.io/en/latest/plugins.html +setenv = + COV_CORE_SOURCE= + COV_CORE_CONFIG={toxinidir}/.coveragerc + COV_CORE_DATAFILE={toxinidir}/.coverage.eager + commands = check-manifest python setup.py sdist twine check dist/* flake8 . - py.test --cov=pgwui_testing tests/ + py.test --cov --cov-append tests/ # coverage run --source src/testing -m py.test # coverage report @@ -28,3 +40,6 @@ commands = exclude = .tox,*.egg,build,data,devel select = E,W,F ignore = W503 + +[tool:pytest] +addopts = --cov --cov-append \ No newline at end of file -- 2.34.1