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.
This commit is contained in:
Pavel Raiskup 2024-10-03 16:13:42 +02:00 committed by Jakub Kadlčík
parent 3ec27836f3
commit 6feb4f2d21
8 changed files with 31 additions and 63 deletions

2
.gitignore vendored
View file

@ -4,7 +4,6 @@
*$py.class *$py.class
*.swp *.swp
*.swo *.swo
.noseids
*.patch *.patch
*# *#
*~ *~
@ -14,6 +13,7 @@ MANIFEST
dist dist
build build
.build .build
.coverage
titorc.5 titorc.5
titorc.5.xml titorc.5.xml
tito.8 tito.8

View file

@ -86,8 +86,8 @@ an authoring environment, too.
To run all tests, install these packages: To run all tests, install these packages:
* python-nose, python-pep8, python-mock (for epl-6 and fedora) and rpm-python * pytest, 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, python3-pep8, python3-mock (for epl-6 and fedora) , and rpm-python3
* createrepo_c * createrepo_c
* git-annex * 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: Then from the root of the project:
python ./runtests.py -vv ./runtests.sh
python3 ./runtests.py -vv
### Advanced ### Advanced

3
pytest.ini Normal file
View file

@ -0,0 +1,3 @@
[pytest]
python_files = test/*.py test/*/*.py
pythonpath = src

View file

@ -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()

10
runtests.sh Executable file
View file

@ -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[@]}"

View file

@ -20,8 +20,8 @@ import glob
import tempfile import tempfile
import sys import sys
import shutil import shutil
from nose.plugins.skip import SkipTest
from os.path import join from os.path import join
from pytest import skip
from functional.fixture import TitoGitTestFixture, tito from functional.fixture import TitoGitTestFixture, tito
@ -41,12 +41,9 @@ class GitAnnexBuilderTests(TitoGitTestFixture):
# Guess based on python version. # Guess based on python version.
# Do not use anything based on uname in case we are in container. # Do not use anything based on uname in case we are in container.
# Do not use `lsb_release` to avoid dependencies. # 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') status, ga_version = getstatusoutput('rpm -q git-annex')
if status != 0: if status != 0:
raise SkipTest("git-annex is missing") skip("git-annex is missing")
# Setup test config: # Setup test config:
self.config = RawConfigParser() self.config = RawConfigParser()

View file

@ -220,9 +220,11 @@ class CommonTests(unittest.TestCase):
def test_colors(self, mock_user_conf): def test_colors(self, mock_user_conf):
mock_user_conf.return_value = {} mock_user_conf.return_value = {}
stream = StringIO() stream = StringIO()
_out('Hello world', None, Terminal().red, stream) with patch("blessed.terminal.os.isatty") as isatty:
# RHEL 6 doesn't have self.assertRegexpMatches unfortunately _out('Hello world', None, Terminal().red, stream)
self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue())) 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): def test_get_project_name(self):
TAGS = [ TAGS = [

View file

@ -17,5 +17,10 @@ skipsdist = True
[testenv] [testenv]
deps = nose deps =
commands = nosetests {posargs} -rrequirements.txt
coverage
pytest
pytest-cov
commands =
python -m pytest -v {posargs} --cov-report term-missing --cov-branch --cov