From a5c3338b0d61302c1a626123a9b383fd2cd3bb1e Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Thu, 3 Oct 2024 16:13:42 +0200 Subject: [PATCH] tests: s/nosetests/pytest/ - Requires us to add pytest.ini to tell pytest where to look for test files, and set the python path. - Mock drops terminal from the tested code, so we have to "mock" it in test_colors() - Enable coverage in tox tests. --- .gitignore | 2 +- HACKING.md | 7 ++-- pytest.ini | 3 ++ runtests.py | 48 ------------------------- runtests.sh | 10 ++++++ test/functional/build_gitannex_tests.py | 7 ++-- test/unit/common_tests.py | 8 +++-- tox.ini | 5 +-- 8 files changed, 27 insertions(+), 63 deletions(-) create mode 100644 pytest.ini delete mode 100755 runtests.py create mode 100755 runtests.sh diff --git a/.gitignore b/.gitignore index b33f56a5..2fa2b44e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ *$py.class *.swp *.swo -.noseids *.patch *# *~ @@ -14,6 +13,7 @@ MANIFEST dist build .build +.coverage titorc.5 titorc.5.xml tito.8 diff --git a/HACKING.md b/HACKING.md index e0197c34..4ba3dab9 100644 --- a/HACKING.md +++ b/HACKING.md @@ -86,8 +86,8 @@ an authoring environment, too. To run all tests, install these packages: -* python-nose, python-pep8, python-mock (for epl-6 and fedora) and rpm-python -* python3-nose, python3-pep8, python3-mock (for epl-6 and fedora) , and rpm-python3 +* pytest, python-pep8, python-mock (for epl-6 and fedora) and rpm-python +* pytest, python3-pep8, python3-mock (for epl-6 and fedora) , and rpm-python3 * createrepo_c * git-annex @@ -98,8 +98,7 @@ for python 2.4 - 2.7 (in case you don't have pip, install via yum python-pip pac Then from the root of the project: - python ./runtests.py -vv - python3 ./runtests.py -vv + ./runtests.sh ### Advanced diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..68214f53 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +python_files = test/*.py test/*/*.py +pythonpath = src diff --git a/runtests.py b/runtests.py deleted file mode 100755 index 604568ba..00000000 --- a/runtests.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/python3 -# -# Copyright (c) 2008-2009 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public License, -# version 2 (GPLv2). There is NO WARRANTY for this software, express or -# implied, including the implied warranties of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 -# along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. -# -# Red Hat trademarks are not licensed under GPLv2. No permission is -# granted to use or replicate Red Hat trademarks that are incorporated -# in this software or its documentation. -""" -Executes all tests. -""" -import os -import shutil -import sys -import tempfile -import nose - -# Make sure we run from the source, this is tricky because the functional -# tests need to find both the location of the 'tito' executable script, -# and the internal tito code needs to know where to find our auxiliary Perl -# scripts. Adding an environment variable hack to the actual code to -# accommodate this for now. - -TEST_SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) -SRC_DIR = os.path.normpath(os.path.join(TEST_SCRIPT_DIR, "src/")) -sys.path.insert(0, SRC_DIR) -SRC_BIN_DIR = os.path.abspath(os.path.join(TEST_SCRIPT_DIR, "bin/")) - -os.environ['TITO_SRC_BIN_DIR'] = SRC_BIN_DIR - -if __name__ == '__main__': - - print("Using Python %s" % sys.version) - print("Using nose %s" % nose.__version__) - print("Running tito tests against: %s" % SRC_DIR) - - # Make sure no older test directories exist - for dir in os.listdir(tempfile.gettempdir()): - if dir.endswith('-titotest'): - shutil.rmtree(os.path.join(tempfile.gettempdir(), dir)) - - nose.main() diff --git a/runtests.sh b/runtests.sh new file mode 100755 index 00000000..c2eb37f8 --- /dev/null +++ b/runtests.sh @@ -0,0 +1,10 @@ +#! /bin/bash -e +cov=--cov +args=() +for arg; do +case $arg in +--no-cov) cov= ;; +*) args+=( "$arg" ) +esac +done +exec python3 -m pytest -vv $cov "${args[@]}" diff --git a/test/functional/build_gitannex_tests.py b/test/functional/build_gitannex_tests.py index 9706d82b..0abb9428 100644 --- a/test/functional/build_gitannex_tests.py +++ b/test/functional/build_gitannex_tests.py @@ -20,8 +20,8 @@ import tempfile import sys import shutil -from nose.plugins.skip import SkipTest from os.path import join +from pytest import skip from functional.fixture import TitoGitTestFixture, tito @@ -41,12 +41,9 @@ def setUp(self): # Guess based on python version. # Do not use anything based on uname in case we are in container. # Do not use `lsb_release` to avoid dependencies. - if sys.version[0:3] == '2.4': - raise SkipTest('git-annex is not available in epel-5') - status, ga_version = getstatusoutput('rpm -q git-annex') if status != 0: - raise SkipTest("git-annex is missing") + skip("git-annex is missing") # Setup test config: self.config = RawConfigParser() diff --git a/test/unit/common_tests.py b/test/unit/common_tests.py index 31625ebf..6a915103 100644 --- a/test/unit/common_tests.py +++ b/test/unit/common_tests.py @@ -220,9 +220,11 @@ def test_turn_off_colors(self, mock_user_conf): def test_colors(self, mock_user_conf): mock_user_conf.return_value = {} stream = StringIO() - _out('Hello world', None, Terminal().red, stream) - # RHEL 6 doesn't have self.assertRegexpMatches unfortunately - self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue())) + with patch("blessed.terminal.os.isatty") as isatty: + _out('Hello world', None, Terminal().red, stream) + isatty.return_value = True + # RHEL 6 doesn't have self.assertRegexpMatches unfortunately + self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue())) def test_get_project_name(self): TAGS = [ diff --git a/tox.ini b/tox.ini index ef40c28a..faa01b53 100644 --- a/tox.ini +++ b/tox.ini @@ -17,5 +17,6 @@ skipsdist = True [testenv] -deps = nose -commands = nosetests {posargs} +deps = pytest pytest-cov +commands = + python -m pytest -v {posargs} --cov-report term-missing --cov-branch --cov