Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor test cleanup: skipif tests that require optional dependencies, migrate unittest skipif to pytest, minor import/typing clean up #742

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/monty/multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from __future__ import annotations

from multiprocessing import Pool
from typing import Callable, Iterable
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Callable, Iterable

try:
from tqdm.autonotebook import tqdm
Expand Down
4 changes: 2 additions & 2 deletions src/monty/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Iterable, cast
from typing import TYPE_CHECKING, cast

if TYPE_CHECKING:
from typing import Any, Union
from typing import Any, Iterable, Union


def remove_non_ascii(s: str) -> str:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_design_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import gc
import pickle
import weakref
from typing import Any
from typing import TYPE_CHECKING

import pytest

from monty.design_patterns import cached_class, singleton

if TYPE_CHECKING:
from typing import Any


class TestSingleton:
def test_singleton(self):
Expand Down
7 changes: 6 additions & 1 deletion tests/test_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import unittest
import warnings
from dataclasses import dataclass
from unittest.mock import patch

import pytest

from monty.dev import deprecated, install_excepthook, requires

try:
from IPython.core import ultratb
except ImportError:
ultratb = None

# Set all warnings to always be triggered.
warnings.simplefilter("always")

Expand Down Expand Up @@ -200,5 +204,6 @@ def use_import_error():
use_import_error()


@pytest.mark.skipif(ultratb is None, reason="ipython is not installed")
def test_install_except_hook():
install_excepthook()
Binary file removed tests/test_files/test_settings.yaml.gz
Binary file not shown.
3 changes: 1 addition & 2 deletions tests/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import platform
import time
import unittest

import pytest

Expand Down Expand Up @@ -620,7 +619,7 @@ def return_none(self):


class TestTimeout:
@unittest.skipIf(platform.system() == "Windows", "Skip on windows")
@pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows")
def test_with(self):
try:
with timeout(1, "timeout!"):
Expand Down
2 changes: 0 additions & 2 deletions tests/test_itertools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright (c) Materials Virtual Lab.
# Distributed under the terms of the BSD License.
from __future__ import annotations

from monty.itertools import chunks, iterator_from_slice
Expand Down
11 changes: 8 additions & 3 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
except ImportError:
ObjectId = None

try:
from bson import json_util
except ImportError:
json_util = None


TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files")


Expand Down Expand Up @@ -969,7 +975,7 @@ def test_redirect(self):
json.loads(json.dumps(d2), cls=MontyDecoder)

def test_redirect_settings_file(self):
data = _load_redirect(os.path.join(TEST_DIR, "test_settings.yaml"))
data = _load_redirect(os.path.join(TEST_DIR, "settings_for_test.yaml"))
assert data == {
"old_module": {
"old_class": {"@class": "new_class", "@module": "new_module"}
Expand Down Expand Up @@ -1255,9 +1261,8 @@ def test_pint(self):
assert isinstance(qty, pint.Quantity)

@pytest.mark.skipif(ObjectId is None, reason="bson not present")
@pytest.mark.skipif(json_util is None, reason="pymongo not present")
def test_extended_json(self):
from bson import json_util

ext_json_dict = {
"datetime": datetime.datetime.now(datetime.timezone.utc),
"NaN": float("NaN"),
Expand Down
10 changes: 9 additions & 1 deletion tests/test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

from math import sqrt

from monty.multiprocessing import imap_tqdm
import pytest

try:
from tqdm.autonotebook import tqdm

from monty.multiprocessing import imap_tqdm
except ImportError:
tqdm = None


@pytest.mark.skipif(tqdm is None, reason="tqdm not installed")
def test_imap_tqdm():
results = imap_tqdm(4, sqrt, range(10_000))
assert len(results) == 10_000
Expand Down
5 changes: 4 additions & 1 deletion tests/test_os.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from __future__ import annotations

import os
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from monty.os import cd, makedirs_p
from monty.os.path import find_exts, zpath

if TYPE_CHECKING:
from pathlib import Path

MODULE_DIR = os.path.dirname(__file__)
TEST_DIR = os.path.join(MODULE_DIR, "test_files")

Expand Down
3 changes: 1 addition & 2 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import glob
import json
import os
import unittest

import pytest

Expand Down Expand Up @@ -66,7 +65,7 @@ def test_dumpfn_loadfn(self):
with pytest.raises(TypeError):
loadfn("monte_test.txt", fmt="garbage")

@unittest.skipIf(msgpack is None, "msgpack-python not installed.")
@pytest.mark.skipif(msgpack is None, reason="msgpack-python not installed.")
def test_mpk(self):
d = {"hello": "world"}

Expand Down
9 changes: 4 additions & 5 deletions tests/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import platform
import shutil
import tempfile
import unittest
from gzip import GzipFile
from pathlib import Path

Expand Down Expand Up @@ -189,21 +188,21 @@ def teardown_method(self):


class TestRemove:
@unittest.skipIf(platform.system() == "Windows", "Skip on windows")
@pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows")
def test_remove_file(self):
tempdir = tempfile.mkdtemp(dir=TEST_DIR)
tempf = tempfile.mkstemp(dir=tempdir)[1]
remove(tempf)
assert not os.path.isfile(tempf)
shutil.rmtree(tempdir)

@unittest.skipIf(platform.system() == "Windows", "Skip on windows")
@pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows")
def test_remove_folder(self):
tempdir = tempfile.mkdtemp(dir=TEST_DIR)
remove(tempdir)
assert not os.path.isdir(tempdir)

@unittest.skipIf(platform.system() == "Windows", "Skip on windows")
@pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows")
def test_remove_symlink(self):
tempdir = tempfile.mkdtemp(dir=TEST_DIR)
tempf = tempfile.mkstemp(dir=tempdir)[1]
Expand All @@ -216,7 +215,7 @@ def test_remove_symlink(self):
assert not os.path.islink(templink)
remove(tempdir)

@unittest.skipIf(platform.system() == "Windows", "Skip on windows")
@pytest.mark.skipif(platform.system() == "Windows", reason="Skip on windows")
def test_remove_symlink_follow(self):
tempdir = tempfile.mkdtemp(dir=TEST_DIR)
tempf = tempfile.mkstemp(dir=tempdir)[1]
Expand Down
8 changes: 2 additions & 6 deletions tests/test_string.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"""
TODO: Modify unittest doc.
"""

from __future__ import annotations

import random
Expand All @@ -10,7 +6,7 @@


def test_remove_non_ascii():
s = "".join(chr(random.randint(0, 127)) for i in range(10))
s += "".join(chr(random.randint(128, 150)) for i in range(10))
s = "".join(chr(random.randint(0, 127)) for _ in range(10))
s += "".join(chr(random.randint(128, 150)) for _ in range(10))
clean = remove_non_ascii(s)
assert len(clean) == 10
4 changes: 0 additions & 4 deletions tests/test_termcolor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"""
TODO: Modify unittest doc.
"""

from __future__ import annotations

import os
Expand Down
Loading