From 32dc63552b849a203d1099120de71c79069f3eab Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 4 Jan 2021 03:26:44 +0000 Subject: [PATCH] tests: lots of tidying --- tests/py37_asyncio.py | 4 +--- tests/tests_asyncio.py | 4 ++-- tests/tests_concurrent.py | 6 +++--- tests/tests_contrib.py | 2 +- tests/tests_keras.py | 10 ++++------ tests/tests_main.py | 4 +--- tests/tests_pandas.py | 8 +++----- tests/tests_perf.py | 4 +--- tests/tests_synchronisation.py | 2 +- tests/tests_tqdm.py | 26 +++++++++++--------------- 10 files changed, 28 insertions(+), 42 deletions(-) diff --git a/tests/py37_asyncio.py b/tests/py37_asyncio.py index cdf0c71b..61ab8a78 100644 --- a/tests/py37_asyncio.py +++ b/tests/py37_asyncio.py @@ -3,10 +3,8 @@ from sys import platform from time import time import asyncio -from pytest import mark - from tqdm.asyncio import tqdm_asyncio, tarange -from .tests_tqdm import StringIO, closing +from .tests_tqdm import StringIO, closing, mark tqdm = partial(tqdm_asyncio, miniters=0, mininterval=0) trange = partial(tarange, miniters=0, mininterval=0) diff --git a/tests/tests_asyncio.py b/tests/tests_asyncio.py index 30f85d7c..6f089264 100644 --- a/tests/tests_asyncio.py +++ b/tests/tests_asyncio.py @@ -4,8 +4,8 @@ import sys if sys.version_info[:2] > (3, 6): from .py37_asyncio import * # NOQA, pylint: disable=wildcard-import else: - import pytest + from .tests_tqdm import skip try: - pytest.skip("async not supported", allow_module_level=True) + skip("async not supported", allow_module_level=True) except TypeError: pass diff --git a/tests/tests_concurrent.py b/tests/tests_concurrent.py index 263c7ec6..3ff4520b 100644 --- a/tests/tests_concurrent.py +++ b/tests/tests_concurrent.py @@ -1,10 +1,10 @@ """ Tests for `tqdm.contrib.concurrent`. """ -from pytest import mark, warns +from pytest import warns from tqdm.contrib.concurrent import thread_map, process_map -from .tests_tqdm import importorskip, skip, StringIO, closing, TqdmWarning +from .tests_tqdm import StringIO, closing, TqdmWarning, importorskip, mark, skip def incr(x): @@ -40,7 +40,7 @@ def test_process_map(): (['x' * 100, ('x',) * 1001], True)]) def test_chunksize_warning(iterables, should_warn): """Test contrib.concurrent.process_map chunksize warnings""" - patch = importorskip("unittest.mock").patch + patch = importorskip('unittest.mock').patch with patch('tqdm.contrib.concurrent._executor_map'): if should_warn: warns(TqdmWarning, process_map, incr, *iterables) diff --git a/tests/tests_contrib.py b/tests/tests_contrib.py index c1596130..3907003f 100644 --- a/tests/tests_contrib.py +++ b/tests/tests_contrib.py @@ -28,7 +28,7 @@ def test_enumerate(): def test_enumerate_numpy(): """Test contrib.tenumerate(numpy.ndarray)""" - np = importorskip("numpy") + np = importorskip('numpy') with closing(StringIO()) as our_file: a = np.random.random((42, 7)) assert list(tenumerate(a, file=our_file)) == list(np.ndenumerate(a)) diff --git a/tests/tests_keras.py b/tests/tests_keras.py index 05c5a682..8cf77132 100644 --- a/tests/tests_keras.py +++ b/tests/tests_keras.py @@ -1,21 +1,19 @@ from __future__ import division -from pytest import mark - from tqdm import tqdm -from .tests_tqdm import importorskip, StringIO, closing +from .tests_tqdm import StringIO, closing, importorskip, mark pytestmark = mark.slow @mark.filterwarnings("ignore:.*:DeprecationWarning") def test_keras(): """Test tqdm.keras.TqdmCallback""" - TqdmCallback = importorskip("tqdm.keras").TqdmCallback - np = importorskip("numpy") + TqdmCallback = importorskip('tqdm.keras').TqdmCallback + np = importorskip('numpy') try: import keras as K except ImportError: - K = importorskip("tensorflow.keras") + K = importorskip('tensorflow.keras') # 1D autoencoder dtype = np.float32 diff --git a/tests/tests_main.py b/tests/tests_main.py index 2be51ea3..e876f88c 100644 --- a/tests/tests_main.py +++ b/tests/tests_main.py @@ -5,11 +5,9 @@ import logging import sys import subprocess -from pytest import mark, raises - from tqdm.cli import main, TqdmKeyError, TqdmTypeError from tqdm.utils import IS_WIN -from .tests_tqdm import _range, closing, BytesIO +from .tests_tqdm import _range, BytesIO, closing, mark, raises def restore_sys(func): diff --git a/tests/tests_pandas.py b/tests/tests_pandas.py index f211f12c..19cdb522 100644 --- a/tests/tests_pandas.py +++ b/tests/tests_pandas.py @@ -1,13 +1,11 @@ from tqdm import tqdm -from .tests_tqdm import importorskip, skip, StringIO, closing - -from pytest import mark +from .tests_tqdm import StringIO, closing, importorskip, mark, skip pytestmark = mark.slow -random = importorskip("numpy.random") +random = importorskip('numpy.random') rand = random.rand randint = random.randint -pd = importorskip("pandas") +pd = importorskip('pandas') def test_pandas_setup(): diff --git a/tests/tests_perf.py b/tests/tests_perf.py index 247b727f..39e7059d 100644 --- a/tests/tests_perf.py +++ b/tests/tests_perf.py @@ -10,10 +10,8 @@ except ImportError: from time import clock process_time = clock -from pytest import mark - from tqdm import tqdm, trange -from .tests_tqdm import importorskip, skip, _range, patch_lock +from .tests_tqdm import _range, importorskip, mark, patch_lock, skip pytestmark = mark.slow diff --git a/tests/tests_synchronisation.py b/tests/tests_synchronisation.py index 2bf5ada9..c6cc1ea7 100644 --- a/tests/tests_synchronisation.py +++ b/tests/tests_synchronisation.py @@ -209,7 +209,7 @@ def test_imap(): @patch_lock(thread=True) def test_threadpool(): """Test concurrent.futures.ThreadPoolExecutor""" - ThreadPoolExecutor = importorskip("concurrent.futures").ThreadPoolExecutor + ThreadPoolExecutor = importorskip('concurrent.futures').ThreadPoolExecutor with ThreadPoolExecutor(8) as pool: try: diff --git a/tests/tests_tqdm.py b/tests/tests_tqdm.py index 02eab10d..8defaf98 100644 --- a/tests/tests_tqdm.py +++ b/tests/tests_tqdm.py @@ -8,8 +8,7 @@ import re import os from functools import wraps from contextlib import contextmanager -from pytest import raises as assert_raises -from pytest import importorskip, skip +from pytest import importorskip, mark, raises, skip from warnings import catch_warnings, simplefilter from tqdm import tqdm @@ -798,15 +797,15 @@ def test_smoothed_dynamic_min_iters_with_min_interval(): assert '14%' in out and '14%' in out2 +@mark.slow def test_rlock_creation(): """Test that importing tqdm does not create multiprocessing objects.""" - import multiprocessing as mp - if sys.version_info < (3, 3): - skip("unittest.mock is a 3.3+ feature") + importorskip('multiprocessing') + from multiprocessing import get_context # Use 'spawn' instead of 'fork' so that the process does not inherit any # globals that have been constructed by running other tests - ctx = mp.get_context('spawn') + ctx = get_context('spawn') with ctx.Pool(1) as pool: # The pool will propagate the error if the target method fails pool.apply(_rlock_creation_target) @@ -814,8 +813,8 @@ def test_rlock_creation(): def _rlock_creation_target(): """Check that the RLock has not been constructed.""" - from unittest.mock import patch import multiprocessing as mp + patch = importorskip('unittest.mock').patch # Patch the RLock class/method but use the original implementation with patch('multiprocessing.RLock', wraps=mp.RLock) as rlock_mock: @@ -1087,10 +1086,9 @@ def test_smoothing(): assert a2 <= c2 <= b2 +@mark.skipif(nt_and_no_colorama, reason="Windows without colorama") def test_deprecated_nested(): """Test nested progress bars""" - if nt_and_no_colorama: - skip("Windows without colorama") # TODO: test degradation on windows without colorama? # Artificially test nested loop printing @@ -1188,11 +1186,9 @@ def test_reset(): assert '| 10/12' in our_file.getvalue() +@mark.skipif(nt_and_no_colorama, reason="Windows without colorama") def test_position(): """Test positioned progress bars""" - if nt_and_no_colorama: - skip("Windows without colorama") - # Artificially test nested loop printing # Without leave our_file = StringIO() @@ -1553,7 +1549,7 @@ def test_write(): def test_len(): """Test advance len (numpy array shape)""" - np = importorskip("numpy") + np = importorskip('numpy') with closing(StringIO()) as f: with tqdm(np.zeros((3, 4)), file=f) as t: assert len(t) == 3 @@ -1585,8 +1581,8 @@ def test_deprecation_exception(): def test_TqdmDeprecationWarning_nofpwrite(): raise TqdmDeprecationWarning('Test!', fp_write=None) - assert_raises(TqdmDeprecationWarning, test_TqdmDeprecationWarning) - assert_raises(Exception, test_TqdmDeprecationWarning_nofpwrite) + raises(TqdmDeprecationWarning, test_TqdmDeprecationWarning) + raises(Exception, test_TqdmDeprecationWarning_nofpwrite) def test_postfix():