Skip to content

Commit

Permalink
tests: lots of tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Jan 4, 2021
1 parent 99fa82d commit 32dc635
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 42 deletions.
4 changes: 1 addition & 3 deletions tests/py37_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
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)
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
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
6 changes: 3 additions & 3 deletions tests/tests_concurrent.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 4 additions & 6 deletions tests/tests_keras.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions tests/tests_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
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):
Expand Down
8 changes: 3 additions & 5 deletions tests/tests_pandas.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
4 changes: 1 addition & 3 deletions tests/tests_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
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


Expand Down
2 changes: 1 addition & 1 deletion tests/tests_synchronisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 11 additions & 15 deletions tests/tests_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
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
Expand Down Expand Up @@ -798,24 +797,24 @@ 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)


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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1585,8 +1581,8 @@ def test_TqdmDeprecationWarning():
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():
Expand Down

0 comments on commit 32dc635

Please sign in to comment.