Skip to content

Commit

Permalink
Don't needlessly inherit from unittest.TestCase (#308)
Browse files Browse the repository at this point in the history
* don't needlessly inherit from unittest.TestCase

i.e. not needed if there are no setUp and tearDown methods

* refactor test path construction to f-strings

prev os.path.join()

* more cleanup
  • Loading branch information
janosh authored Nov 17, 2023
1 parent af0c766 commit 7284ec2
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 396 deletions.
12 changes: 2 additions & 10 deletions tests/ansible/test_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Created on Jun 1, 2012
"""
"""Created on Jun 1, 2012."""


__author__ = "Shyue Ping Ong"
Expand All @@ -10,15 +8,14 @@
__email__ = "shyue@mit.edu"
__date__ = "Jun 1, 2012"

import unittest

import pytest

from custodian.ansible.actions import FileActions
from custodian.ansible.interpreter import Modder


class ModderTest(unittest.TestCase):
class TestModder:
def test_dict_modify(self):
modder = Modder()
dct = {"Hello": "World"}
Expand Down Expand Up @@ -159,8 +156,3 @@ def as_dict(self):
@staticmethod
def from_dict(dct):
return MyObject(dct["b"]["a"])


if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()
70 changes: 35 additions & 35 deletions tests/feff/test_jobs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
__author__ = "Chen Zheng"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "Chen Zheng"
__email__ = "chz022@ucsd.edu"
__date__ = "Oct 18, 2017"

import os
import unittest

from monty.os import cd
from monty.tempfile import ScratchDir
Expand All @@ -15,33 +7,41 @@
from custodian import TEST_FILES
from custodian.feff.jobs import FeffJob

__author__ = "Chen Zheng"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "Chen Zheng"
__email__ = "chz022@ucsd.edu"
__date__ = "Oct 18, 2017"

TEST_DIR = f"{TEST_FILES}/feff_unconverged"


class FeffJobTest(unittest.TestCase):
def test_to_from_dict(self):
f = FeffJob("hello")
f2 = FeffJob.from_dict(f.as_dict())
assert type(f) == type(f2)
assert f2.feff_cmd == "hello"

def test_setup(self):
with cd(TEST_DIR), ScratchDir(".", copy_from_current_on_enter=True):
f = FeffJob("hello", backup=True)
f.setup()

parameter = Tags.from_file("feff.inp")
parameter_orig = Tags.from_file("feff.inp.orig")
assert parameter == parameter_orig

atom = Atoms.cluster_from_file("feff.inp")
atom_origin = Atoms.cluster_from_file("feff.inp.orig")
assert atom == atom_origin

def test_postprocess(self):
with cd(TEST_DIR), ScratchDir(".", copy_from_current_on_enter=True):
f = FeffJob("hello", backup=True, gzipped=True)
f.postprocess()
assert os.path.exists("feff_out.1.tar.gz")
f.postprocess()
assert os.path.exists("feff_out.2.tar.gz")
def test_to_from_dict():
f = FeffJob("hello")
f2 = FeffJob.from_dict(f.as_dict())
assert type(f) == type(f2)
assert f2.feff_cmd == "hello"


def test_setup():
with cd(TEST_DIR), ScratchDir(".", copy_from_current_on_enter=True):
f = FeffJob("hello", backup=True)
f.setup()

parameter = Tags.from_file("feff.inp")
parameter_orig = Tags.from_file("feff.inp.orig")
assert parameter == parameter_orig

atom = Atoms.cluster_from_file("feff.inp")
atom_origin = Atoms.cluster_from_file("feff.inp.orig")
assert atom == atom_origin


def test_postprocess():
with cd(TEST_DIR), ScratchDir(".", copy_from_current_on_enter=True):
f = FeffJob("hello", backup=True, gzipped=True)
f.postprocess()
assert os.path.exists("feff_out.1.tar.gz")
f.postprocess()
assert os.path.exists("feff_out.2.tar.gz")
7 changes: 3 additions & 4 deletions tests/lobster/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import unittest

from custodian import TEST_FILES
from custodian.lobster.handlers import ChargeSpillingValidator, EnoughBandsValidator, LobsterFilesValidator

test_files_lobster = f"{TEST_FILES}/lobster/lobsterouts"


class TestChargeSpillingValidator(unittest.TestCase):
class TestChargeSpillingValidator:
def test_check_and_correct(self):
v = ChargeSpillingValidator(output_filename=f"{test_files_lobster}/lobsterout.normal")
assert not v.check()
Expand All @@ -31,7 +30,7 @@ def test_as_dict(self):
assert isinstance(v2, ChargeSpillingValidator)


class TestLobsterFilesValidator(unittest.TestCase):
class TestLobsterFilesValidator:
def test_check_and_correct_1(self):
os.chdir(test_files_lobster)
v = LobsterFilesValidator()
Expand All @@ -55,7 +54,7 @@ def test_as_dict(self):
assert isinstance(v2, LobsterFilesValidator)


class TestEnoughBandsValidator(unittest.TestCase):
class TestEnoughBandsValidator:
def test_check_and_correct(self):
v = EnoughBandsValidator(output_filename=f"{test_files_lobster}/lobsterout.normal")
assert not v.check()
Expand Down
19 changes: 9 additions & 10 deletions tests/lobster/test_jobs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import shutil
import unittest

from monty.os import cd
from monty.tempfile import ScratchDir
Expand All @@ -11,7 +10,7 @@
test_files_lobster2 = f"{TEST_FILES}/lobster/lobsterins"
test_files_lobster3 = f"{TEST_FILES}/lobster/vasp_lobster_output"

VASP_OUTPUT_FILES = [
VASP_OUTPUT_FILES = (
"OUTCAR",
"vasprun.xml",
"CHG",
Expand All @@ -30,11 +29,11 @@
"REPORT",
"WAVECAR",
"XDATCAR",
]
)

LOBSTERINPUT_FILES = ["lobsterin"]
LOBSTERINPUT_FILES = ("lobsterin",)

LOBSTER_FILES = [
LOBSTER_FILES = (
"lobsterin",
"lobsterin.orig",
"lobsterout",
Expand All @@ -53,12 +52,12 @@
"ICOBILIST.lobster",
"COBICAR.lobster",
"DOSCAR.LSO.lobster",
]
)

FW_FILES = ["custodian.json", "FW.json", "FW_submit.script"]
FW_FILES = ("custodian.json", "FW.json", "FW_submit.script")


class LobsterJobTest(unittest.TestCase):
class TestLobsterJob:
"""Similar to VaspJobTest. Omit test of run."""

def test_to_from_dict(self):
Expand Down Expand Up @@ -87,13 +86,13 @@ def test_postprocess(self):
shutil.copy("lobsterin", "lobsterin.orig")
v = LobsterJob("hello", gzipped=True, add_files_to_gzip=VASP_OUTPUT_FILES)
v.postprocess()
for file in VASP_OUTPUT_FILES + LOBSTER_FILES + FW_FILES:
for file in (*VASP_OUTPUT_FILES, *LOBSTER_FILES, *FW_FILES):
filegz = file + ".gz"
assert os.path.exists(filegz)

with ScratchDir(".", copy_from_current_on_enter=True):
shutil.copy("lobsterin", "lobsterin.orig")
v = LobsterJob("hello", gzipped=False, add_files_to_gzip=VASP_OUTPUT_FILES)
v.postprocess()
for file in VASP_OUTPUT_FILES + LOBSTER_FILES + FW_FILES:
for file in (*VASP_OUTPUT_FILES, *LOBSTER_FILES, *FW_FILES):
assert os.path.exists(file)
30 changes: 14 additions & 16 deletions tests/nwchem/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import shutil
import unittest
from glob import glob

from custodian import TEST_FILES
Expand All @@ -14,18 +13,17 @@
__date__ = "6/18/13"


class NwchemErrorHandlerTest(unittest.TestCase):
def test_check_correct(self):
os.chdir(f"{TEST_FILES}/nwchem")
shutil.copy("C1N1Cl1_1.nw", "C1N1Cl1_1.nw.orig")
handler = NwchemErrorHandler(output_filename="C1N1Cl1_1.nwout")
handler.check()
handler.correct()
shutil.move("C1N1Cl1_1.nw.orig", "C1N1Cl1_1.nw")
shutil.copy("Li1_1.nw", "Li1_1.nw.orig")
handler = NwchemErrorHandler(output_filename="Li1_1.nwout")
handler.check()
handler.correct()
shutil.move("Li1_1.nw.orig", "Li1_1.nw")
for f in glob("error.*.tar.gz"):
os.remove(f)
def test_check_correct():
os.chdir(f"{TEST_FILES}/nwchem")
shutil.copy("C1N1Cl1_1.nw", "C1N1Cl1_1.nw.orig")
handler = NwchemErrorHandler(output_filename="C1N1Cl1_1.nwout")
handler.check()
handler.correct()
shutil.move("C1N1Cl1_1.nw.orig", "C1N1Cl1_1.nw")
shutil.copy("Li1_1.nw", "Li1_1.nw.orig")
handler = NwchemErrorHandler(output_filename="Li1_1.nwout")
handler.check()
handler.correct()
shutil.move("Li1_1.nw.orig", "Li1_1.nw")
for f in glob("error.*.tar.gz"):
os.remove(f)
2 changes: 1 addition & 1 deletion tests/qchem/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


TEST_DIR = f"{TEST_FILES}/qchem/new_test_files"
SCR_DIR = os.path.join(TEST_DIR, "scratch")
SCR_DIR = f"{TEST_DIR}/scratch"
CWD = os.getcwd()


Expand Down
2 changes: 1 addition & 1 deletion tests/qchem/test_job_handler_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
__date__ = "6/3/22"

TEST_DIR = f"{TEST_FILES}/qchem/new_test_files"
SCR_DIR = os.path.join(TEST_DIR, "scratch")
SCR_DIR = f"{TEST_DIR}/scratch"
CWD = os.getcwd()


Expand Down
Loading

0 comments on commit 7284ec2

Please sign in to comment.