diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 66a8baa5..b2b039f2 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -40,7 +40,7 @@ jobs: conda install --file requirements/test.txt conda install --file requirements/build.txt python -m pip install -r requirements/pip.txt - python -m pip install -e . --no-deps + python -m pip install . --no-deps - name: Validate diffpy.pdffit2 run: | diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index a6f0e236..89ff2948 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -36,7 +36,7 @@ jobs: conda install --file requirements/run.txt conda install --file requirements/docs.txt python -m pip install -r requirements/pip.txt - python -m pip install -e . --no-deps + python -m pip install . --no-deps - name: build documents run: make -C doc html diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6b42f847..30f4e0a4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,7 +37,7 @@ jobs: conda install --file requirements/test.txt conda install --file requirements/build.txt python -m pip install -r requirements/pip.txt - python -m pip install -e . --no-deps + python -m pip install . --no-deps - name: Validate diffpy.pdffit2 run: python -m pytest diff --git a/.github/workflows/matrix.yml b/.github/workflows/matrix.yml index a4f39eca..3c8c0f0d 100644 --- a/.github/workflows/matrix.yml +++ b/.github/workflows/matrix.yml @@ -42,7 +42,7 @@ jobs: conda install --file requirements/test.txt conda install --file requirements/build.txt python -m pip install -r requirements/pip.txt - python -m pip install -e . --no-deps + python -m pip install . --no-deps - name: Validate diffpy.pdffit2 run: python -m pytest diff --git a/conda-recipe/run_test.py b/conda-recipe/run_test.py deleted file mode 100644 index 1432eed6..00000000 --- a/conda-recipe/run_test.py +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python - -import diffpy.pdffit2.tests - -assert diffpy.pdffit2.tests.test().wasSuccessful() diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index 2fbf7d55..00000000 --- a/pytest.ini +++ /dev/null @@ -1,2 +0,0 @@ -[pytest] -python_files = *Test*.py diff --git a/src/diffpy/pdffit2/tests/__init__.py b/src/diffpy/pdffit2/tests/__init__.py deleted file mode 100644 index a27d592f..00000000 --- a/src/diffpy/pdffit2/tests/__init__.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# (c) 2012 Trustees of the Columbia University in the City of New York. -# All rights reserved. -# (c) 2024 The Trustees of Columbia University in the City of New York. -# All rights reserved. -# -# File coded by: Billinge Group members and community contributors. -# -# See GitHub contributions for a more detailed list of contributors. -# https://github.com/diffpy/diffpy.pdffit2/graphs/contributors -# -# See LICENSE.rst for license information. -# -############################################################################## - -"""Unit tests for the diffpy.pdffit2 package. -""" - -import unittest - - -def testsuite(pattern=""): - """Create a unit tests suite for the diffpy.pdffit2 package. - - Parameters - ---------- - pattern : str, optional - Regular expression pattern for selecting test cases. - Select all tests when empty. Ignore the pattern when - any of unit test modules fails to import. - - Returns - ------- - suite : `unittest.TestSuite` - The TestSuite object containing the matching tests. - """ - import re - from itertools import chain - from os.path import dirname - - from pkg_resources import resource_filename - - loader = unittest.defaultTestLoader - thisdir = resource_filename(__name__, "") - depth = __name__.count(".") + 1 - topdir = thisdir - for i in range(depth): - topdir = dirname(topdir) - suite_all = loader.discover(thisdir, pattern="*Test*.py", top_level_dir=topdir) - # always filter the suite by pattern to test-cover the selection code. - suite = unittest.TestSuite() - rx = re.compile(pattern) - tsuites = list(chain.from_iterable(suite_all)) - tsok = all(isinstance(ts, unittest.TestSuite) for ts in tsuites) - if not tsok: # pragma: no cover - return suite_all - tcases = chain.from_iterable(tsuites) - for tc in tcases: - tcwords = tc.id().split(".") - shortname = ".".join(tcwords[-3:]) - if rx.search(shortname): - suite.addTest(tc) - # verify all tests are found for an empty pattern. - assert pattern or suite_all.countTestCases() == suite.countTestCases() - return suite - - -def test(): - """Execute all unit tests for the diffpy.pdffit2 package. - - Returns - ------- - result : `unittest.TestResult` - """ - suite = testsuite() - runner = unittest.TextTestRunner() - result = runner.run(suite) - return result - - -# End of file diff --git a/src/diffpy/pdffit2/tests/conftest.py b/src/diffpy/pdffit2/tests/conftest.py deleted file mode 100644 index e3b63139..00000000 --- a/src/diffpy/pdffit2/tests/conftest.py +++ /dev/null @@ -1,19 +0,0 @@ -import json -from pathlib import Path - -import pytest - - -@pytest.fixture -def user_filesystem(tmp_path): - base_dir = Path(tmp_path) - home_dir = base_dir / "home_dir" - home_dir.mkdir(parents=True, exist_ok=True) - cwd_dir = base_dir / "cwd_dir" - cwd_dir.mkdir(parents=True, exist_ok=True) - - home_config_data = {"username": "home_username", "email": "home@email.com"} - with open(home_dir / "diffpyconfig.json", "w") as f: - json.dump(home_config_data, f) - - yield tmp_path diff --git a/src/diffpy/pdffit2/tests/debug.py b/src/diffpy/pdffit2/tests/debug.py deleted file mode 100644 index 3e30239f..00000000 --- a/src/diffpy/pdffit2/tests/debug.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# (c) 2024 The Trustees of Columbia University in the City of New York. -# All rights reserved. -# -# File coded by: Billinge Group members and community contributors. -# -# See GitHub contributions for a more detailed list of contributors. -# https://github.com/diffpy/diffpy.pdffit2/graphs/contributors -# -# See LICENSE.rst for license information. -# -############################################################################## - -""" -Convenience module for debugging the unit tests using - -python -m diffpy.pdffit2.tests.debug - -Exceptions raised by failed tests or other errors are not caught. -""" - - -if __name__ == "__main__": - import sys - - from diffpy.pdffit2.tests import testsuite - - pattern = sys.argv[1] if len(sys.argv) > 1 else "" - suite = testsuite(pattern) - suite.debug() - - -# End of file diff --git a/src/diffpy/pdffit2/tests/pdffit2testutils.py b/src/diffpy/pdffit2/tests/pdffit2testutils.py deleted file mode 100644 index ce61aa16..00000000 --- a/src/diffpy/pdffit2/tests/pdffit2testutils.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# diffpy.srreal by DANSE Diffraction group -# Simon J. L. Billinge -# (c) 2010 Trustees of the Columbia University -# in the City of New York. All rights reserved. -# -# File coded by: Pavol Juhas -# -# See AUTHORS.txt for a list of people who contributed. -# See LICENSE.txt for license information. -# -############################################################################## - -"""Helper routines for running other unit tests. -Import of this module suppresses the chatty output from the C++ extension. -""" - - -import os.path - -import six - -import diffpy.pdffit2 - -# silence the C++ engine output -diffpy.pdffit2.redirect_stdout(open(os.path.devnull, "w")) - -# path variables -thisfile = locals().get("__file__", "file.py") -tests_dir = os.path.dirname(os.path.abspath(thisfile)) -testdata_dir = os.path.join(tests_dir, "testdata") - - -def datafile(filename): - """prepend testdata_dir to filename.""" - return os.path.join(testdata_dir, filename) - - -def capture_output(f, *args, **kwargs): - """Capture output from pdffit2 engine produced in function call.""" - savestdout = diffpy.pdffit2.output.stdout - fp = six.StringIO() - diffpy.pdffit2.redirect_stdout(fp) - try: - f(*args, **kwargs) - finally: - diffpy.pdffit2.redirect_stdout(savestdout) - return fp.getvalue() - - -# End of file diff --git a/src/diffpy/pdffit2/tests/run.py b/src/diffpy/pdffit2/tests/run.py deleted file mode 100644 index d5c4ea41..00000000 --- a/src/diffpy/pdffit2/tests/run.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# (c) 2012 Trustees of the Columbia University in the City of New York. -# All rights reserved. -# (c) 2024 The Trustees of Columbia University in the City of New York. -# All rights reserved. -# -# File coded by: Billinge Group members and community contributors. -# -# See GitHub contributions for a more detailed list of contributors. -# https://github.com/diffpy/diffpy.pdffit2/graphs/contributors -# -# See LICENSE.rst for license information. -# -############################################################################## - -"""Convenience module for executing all unit tests with -python -m diffpy.pdffit2.tests.run -""" - - -if __name__ == "__main__": - import sys - - # show warnings by default - if not sys.warnoptions: - import os - import warnings - - warnings.simplefilter("default") - # also affect subprocesses - os.environ["PYTHONWARNINGS"] = "default" - from diffpy.pdffit2.tests import test - - # produce zero exit code for a successful test - sys.exit(not test().wasSuccessful()) - - -# Consider upgrading to pytest -# import sys -# -# import pytest -# -# if __name__ == "__main__": -# # show output results from every test function -# args = ["-v"] -# # show the message output for skipped and expected failure tests -# if len(sys.argv) > 1: -# args.extend(sys.argv[1:]) -# print("pytest arguments: {}".format(args)) -# # call pytest and exit with the return code from pytest -# exit_res = pytest.main(args) -# sys.exit(exit_res) - -# End of file diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..5407f543 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,50 @@ +import json +from pathlib import Path + +import pytest +import six + +import diffpy.pdffit2 +import diffpy.pdffit2.output # assuming this is the correct import path + + +@pytest.fixture +def user_filesystem(tmp_path): + base_dir = Path(tmp_path) + home_dir = base_dir / "home_dir" + home_dir.mkdir(parents=True, exist_ok=True) + cwd_dir = base_dir / "cwd_dir" + cwd_dir.mkdir(parents=True, exist_ok=True) + + home_config_data = {"username": "home_username", "email": "home@email.com"} + with open(home_dir / "diffpyconfig.json", "w") as f: + json.dump(home_config_data, f) + + yield tmp_path + + +@pytest.fixture +def datafile(): + """Fixture to dynamically load any test file.""" + + def _load(filename): + return "tests/testdata/" + filename + + return _load + + +@pytest.fixture +def capture_output(): + """Capture output from pdffit2 engine produced in function call.""" + + def _capture(f, *args, **kwargs): + savestdout = diffpy.pdffit2.output.stdout + fp = six.StringIO() + diffpy.pdffit2.redirect_stdout(fp) + try: + f(*args, **kwargs) + finally: + diffpy.pdffit2.redirect_stdout(savestdout) + return fp.getvalue() + + return _capture diff --git a/src/diffpy/pdffit2/tests/ExceptionsTest.py b/tests/test_exceptions.py similarity index 80% rename from src/diffpy/pdffit2/tests/ExceptionsTest.py rename to tests/test_exceptions.py index 5dee1b75..f2f245ce 100644 --- a/src/diffpy/pdffit2/tests/ExceptionsTest.py +++ b/tests/test_exceptions.py @@ -16,11 +16,16 @@ import unittest +import pytest + from diffpy.pdffit2 import PdfFit, pdffit2 -from diffpy.pdffit2.tests.pdffit2testutils import datafile class read_structExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -33,15 +38,19 @@ def test_IOError(self): def test_structureError(self): """raise pdffit2.structureError when structure is malformed""" - self.assertRaises(pdffit2.structureError, self.P.read_struct, datafile("badNi.stru")) + self.assertRaises(pdffit2.structureError, self.P.read_struct, self.datafile("badNi.stru")) def test_structureErrorZeroVolume(self): """raise pdffit2.structureError when unit cell volume is negative""" # I don't know how to test for this, but it's in the library code - self.assertRaises(pdffit2.structureError, self.P.read_struct, datafile("badNiZeroVolume.stru")) + self.assertRaises(pdffit2.structureError, self.P.read_struct, self.datafile("badNiZeroVolume.stru")) class read_dataExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -54,10 +63,14 @@ def test_IOError(self): def test_dataError(self): """raise pdffit2.dataError when data has improper spacing""" - self.assertRaises(pdffit2.dataError, self.P.read_data, datafile("badNi.dat"), "X", 25.0, 0.5) + self.assertRaises(pdffit2.dataError, self.P.read_data, self.datafile("badNi.dat"), "X", 25.0, 0.5) class read_data_listsExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.r_data = [0.1, 0.2] @@ -95,6 +108,10 @@ def test_dataError(self): class pdfrangeExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.iset = 1 @@ -110,16 +127,20 @@ def test_ValueError1(self): def test_ValueError2(self): """raise ValueError when rmax < rmin""" - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.5) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.5) self.assertRaises(ValueError, self.P.pdfrange, self.iset, self.rmax, self.rmin) def test_ValueError3(self): """raise ValueError when range outside of data""" - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.5) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.5) self.assertRaises(ValueError, self.P.pdfrange, self.iset, -self.rmin, self.rmax) class allocExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.qmax = 25 @@ -133,44 +154,48 @@ def tearDown(self): def test_ValueError1(self): """raise ValueError when qmax < 0""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.alloc, "X", -self.qmax, self.qdamp, self.rmin, self.rmax, self.bin) def test_ValueError2(self): """raise ValueError when qdamp < 0""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.alloc, "X", self.qmax, -self.qdamp, self.rmin, self.rmax, self.bin) def test_ValueError3(self): """raise ValueError when rmin < 0""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.alloc, "X", self.qmax, self.qdamp, -self.rmin, self.rmax, self.bin) def test_ValueError4(self): """raise ValueError when rmax < 0""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.alloc, "X", self.qmax, self.qdamp, self.rmin, -self.rmax, self.bin) def test_ValueError5(self): """raise ValueError when bin < 0""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.alloc, "X", self.qmax, self.qdamp, self.rmin, self.rmax, -self.bin) def test_ValueError6(self): """raise ValueError when rmax < rmin""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.alloc, "X", self.qmax, self.qdamp, self.rmax, self.rmin, self.bin) def test_ValueError7(self): """raise ValueError when qdamp < 0""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.alloc, "X", self.qmax, self.qdamp, self.rmin, self.rmax, -self.bin) class calcExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) def tearDown(self): del self.P @@ -224,6 +249,10 @@ def test_unassignedError(self): class save_pdfExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.strufile = "temp.pdf" @@ -233,7 +262,7 @@ def tearDown(self): def test_IOError(self): """raise IOError when structure cannot be saved""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 30.0, 0.05, 2, 10, 100) self.P.calc() self.assertRaises(IOError, self.P.save_pdf, 1, "nodir183160/" + self.strufile) @@ -244,6 +273,10 @@ def test_unassignedError(self): class save_difExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.strufile = "temp.dif" @@ -253,10 +286,10 @@ def tearDown(self): def test_IOError(self): """raise IOError when dif cannot be saved""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 30.0, 0.05, 2, 10, 100) self.P.calc() - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.5) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.5) self.assertRaises(IOError, self.P.save_dif, 1, "nodir183160/" + self.strufile) def test_unassignedError(self): @@ -265,6 +298,10 @@ def test_unassignedError(self): class save_resExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.resfile = "temp.res" @@ -274,8 +311,8 @@ def tearDown(self): def test_IOError(self): """raise IOError when residual file cannot be saved""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 30.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 30.0, 0.0) self.P.constrain(self.P.lat(1), 1) self.P.setpar(1, 3.0) self.P.pdfrange(1, 2.0, 10.0) @@ -288,6 +325,10 @@ def test_unassignedError(self): class save_structExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + # Same code as show_struct def setUp(self): @@ -299,7 +340,7 @@ def tearDown(self): def test_IOError(self): """raise IOError when structure cannot be saved""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(IOError, self.P.save_struct, 1, "nodir183160/" + self.strufile) def test_unassignedError(self): @@ -308,6 +349,10 @@ def test_unassignedError(self): class constrainExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.par = 1 @@ -319,8 +364,8 @@ def tearDown(self): def test_constraintError(self): """raise constraintError when constraint is bad""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.P.constrain("x(1)", "junk+@1") self.P.setpar(1, 0.01) self.assertRaises(pdffit2.constraintError, self.P.calc) @@ -334,14 +379,14 @@ def test_unassignedError(self): def test_ValueError(self): """raise ValueError when a variable index does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.constrain, self.P.x(6), self.par) return def test_constrainNonRefVar(self): "raise constraintError when attempting to constrain non-refinables" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.assertRaises(pdffit2.constraintError, self.P.constrain, "rcut", "@7") self.assertRaises(pdffit2.constraintError, self.P.constrain, "rcut", 13) self.assertRaises(pdffit2.constraintError, self.P.constrain, "stepcut", "@17") @@ -349,6 +394,10 @@ def test_constrainNonRefVar(self): class setvarExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.val = 3.0 @@ -362,11 +411,15 @@ def test_unassignedError(self): def test_ValueError(self): """raise ValueError when a variable index does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.setvar, self.P.lat(7), self.val) class getvarExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -379,7 +432,7 @@ def test_unassignedError(self): def test_ValueError(self): """raise ValueError when a variable index does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.getvar, self.P.lat(7)) @@ -444,6 +497,10 @@ def test_unassignedError(self): class getparExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -456,7 +513,7 @@ def test_unassignedError1(self): def test_unassignedError2(self): """raise pdffit2.unassignedError when parameter does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.constrain(self.P.lat(1), 2) self.assertRaises(pdffit2.unassignedError, self.P.getpar, 1) @@ -479,6 +536,10 @@ def test_unassignedError2(self): class pdeselExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.ip = 1 @@ -492,11 +553,15 @@ def test_unassignedError1(self): def test_unassignedError2(self): """raise pdffit2.unassignedError when phase does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(pdffit2.unassignedError, self.P.pdesel, self.ip) class selectAtomTypeExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.iset = 1 @@ -511,9 +576,9 @@ def test_unassignedError1(self): def test_unassignedError2(self): """raise pdffit2.unassignedError when set does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) # selectAtomType should pass with one phase defined - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.P.selectAtomType(self.iset, "i", "Ni", True) self.P.selectAtomType(self.iset, "j", "Ni", False) # but fail for phase 2 which is not present @@ -521,14 +586,18 @@ def test_unassignedError2(self): def test_ijcharValueError(self): """raise ValueError when ijchar is neither 'i' nor 'j'""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.P.selectAtomType(self.iset, "i", "Ni", True) self.P.selectAtomType(self.iset, "j", "Ni", True) self.assertRaises(ValueError, self.P.selectAtomType, self.iset, "k", "Ni", True) class selectAtomIndexExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.iset = 1 @@ -543,9 +612,9 @@ def test_unassignedError1(self): def test_unassignedError2(self): """raise pdffit2.unassignedError when set does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) # pass for phase 1 - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.P.selectAtomIndex(self.iset, "i", 1, True) self.P.selectAtomIndex(self.iset, "i", 2, False) # fail for phase 2 @@ -553,12 +622,16 @@ def test_unassignedError2(self): def test_ValueError(self): """raise ValueError when selected atom does not exist""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.assertRaises(ValueError, self.P.selectAtomIndex, self.iset, "i", 6, True) class selectAllExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.iset = 1 @@ -573,11 +646,11 @@ def test_unassignedError1(self): def test_unassignedError2(self): """raise pdffit2.unassignedError when set does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) # fail when there is no dataset self.assertRaises(pdffit2.unassignedError, self.P.selectAll, self.iset, "i") # pass with dataset - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.P.selectAll(self.iset, "i") self.P.selectAll(self.iset, "j") # fail for phase 2 @@ -586,6 +659,10 @@ def test_unassignedError2(self): class selectNoneExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.iset = 1 @@ -600,11 +677,11 @@ def test_unassignedError1(self): def test_unassignedError2(self): """raise pdffit2.unassignedError when set does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) # fail when there is no dataset self.assertRaises(pdffit2.unassignedError, self.P.selectNone, self.iset, "i") # pass with dataset - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.P.selectNone(self.iset, "i") self.P.selectNone(self.iset, "j") # fail for phase 2 @@ -613,6 +690,10 @@ def test_unassignedError2(self): class bangExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.a1 = 1 @@ -628,22 +709,26 @@ def test_unassignedError(self): def test_ValueError1(self): """raise ValueError when selected atom(s) does not exist""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.assertRaises(ValueError, self.P.bang, 0, self.a2, self.a3) def test_ValueError2(self): """raise ValueError when selected atom(s) does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.bang, self.a1, -1, self.a3) def test_ValueError3(self): """raise ValueError when selected atom(s) does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.bang, self.a1, self.a2, 6) class blenExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() self.a1 = 1 @@ -658,17 +743,17 @@ def test_unassignedError(self): def test_ValueError1(self): """raise ValueError when selected atom(s) does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.blen, 0, self.a2) def test_ValueError2(self): """raise ValueError when selected atom(s) does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.blen, self.a1, 6) def test_ValueError3(self): """raise ValueError when selected atom(s) does not exist""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertRaises(ValueError, self.P.blen, 0, 6) @@ -716,6 +801,10 @@ def test_unassignedError(self): class fixparExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -724,12 +813,16 @@ def tearDown(self): def test_unassignedError(self): """raise pdffit2.unassignedError when parameter does not exist""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.assertRaises(pdffit2.unassignedError, self.P.fixpar, 1) class freeparExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -738,12 +831,16 @@ def tearDown(self): def test_unassignedError(self): """raise pdffit2.unassignedError when parameter does not exist""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.assertRaises(pdffit2.unassignedError, self.P.freepar, 1) class setphaseExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -752,12 +849,16 @@ def tearDown(self): def test_unassignedError(self): """raise pdffit2.unassignedError when phase does not exist""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.assertRaises(pdffit2.unassignedError, self.P.setphase, 2) class setdataExceptions(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() @@ -766,8 +867,8 @@ def tearDown(self): def test_unassignedError(self): """raise pdffit2.unassignedError when data set does not exist""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.assertRaises(pdffit2.unassignedError, self.P.setdata, 2) diff --git a/src/diffpy/pdffit2/tests/TestPdfFit.py b/tests/test_pdffit.py similarity index 87% rename from src/diffpy/pdffit2/tests/TestPdfFit.py rename to tests/test_pdffit.py index 70712356..68c06833 100644 --- a/src/diffpy/pdffit2/tests/TestPdfFit.py +++ b/tests/test_pdffit.py @@ -5,8 +5,9 @@ import unittest +import pytest + from diffpy.pdffit2 import PdfFit, pdffit2 -from diffpy.pdffit2.tests.pdffit2testutils import capture_output, datafile from diffpy.structure import loadStructure # ---------------------------------------------------------------------------- @@ -15,6 +16,11 @@ class TestPdfFit(unittest.TestCase): places = 6 + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile, capture_output): + self.datafile = datafile + self.capture_output = capture_output + def setUp(self): self.P = PdfFit() return @@ -42,7 +48,7 @@ def test__exportAll(self): def test_add_structure(self): """check PdfFit.add_structure()""" - ni = loadStructure(datafile("Ni.stru")) + ni = loadStructure(self.datafile("Ni.stru")) self.P.add_structure(ni) self.assertEqual(4, self.P.num_atoms()) return @@ -65,7 +71,7 @@ def test_add_structure(self): def test_read_data_string(self): """check PdfFit.read_data_string()""" pf = self.P - with open(datafile("300K.gr")) as fp: + with open(self.datafile("300K.gr")) as fp: s = fp.read() self.assertEqual([], pf.data_files) pf.read_data_string(s, "N", 32, 0.03, "lmo") @@ -99,7 +105,7 @@ def test_alloc(self): self.P.calc() Gzero = self.P.getpdf_fit() self.assertEqual(1000 * [0.0], Gzero) - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.calc() # check r-values r = self.P.getR() @@ -109,7 +115,7 @@ def test_alloc(self): Gfit_alloc_read = self.P.getpdf_fit() # now try the other order self.P.reset() - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 25, 0.0, 0.01, 10, 1000) self.P.calc() Gfit_read_alloc = self.P.getpdf_fit() @@ -164,8 +170,8 @@ def test_alloc(self): def test_get_structure(self): """check PdfFit.get_structure()""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) stru1 = self.P.get_structure(1) self.assertEqual(4, len(stru1)) self.assertEqual("Ni", stru1[0].element) @@ -197,7 +203,7 @@ def test_get_structure(self): def test_setpar(self): """check PdfFit.setpar()""" pf = self.P - pf.read_struct(datafile("Ni.stru")) + pf.read_struct(self.datafile("Ni.stru")) pf.setpar(1, "lat(1)") self.assertEqual(3.52, pf.getpar(1)) pf.setpar(1, 4.0) @@ -209,7 +215,7 @@ def test_setpar(self): def test_setvar(self): """check PdfFit.setvar()""" pf = self.P - pf.read_struct(datafile("Ni.stru")) + pf.read_struct(self.datafile("Ni.stru")) pf.setvar(pf.delta1, 1.2) self.assertEqual(1.2, pf.getvar(pf.delta1)) pf.setvar("delta1", 1.7) @@ -248,8 +254,8 @@ def test_setvar(self): def test_get_atoms(self): """check PdfFit.get_atoms()""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.setphase(1) a1 = self.P.get_atoms() a2 = self.P.get_atoms(2) @@ -259,8 +265,8 @@ def test_get_atoms(self): def test_get_atom_types(self): """check PdfFit.get_atom_types()""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.setphase(1) atp1 = self.P.get_atom_types() atp2 = self.P.get_atom_types(2) @@ -271,9 +277,9 @@ def test_get_atom_types(self): def test_num_phases(self): """check PdfFit.num_phases()""" self.assertEqual(0, self.P.num_phases()) - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertEqual(1, self.P.num_phases()) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.assertEqual(2, self.P.num_phases()) self.P.reset() self.assertEqual(0, self.P.num_phases()) @@ -282,17 +288,17 @@ def test_num_phases(self): def test_num_datasets(self): """check PdfFit.num_datasets()""" self.assertEqual(0, self.P.num_datasets()) - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.5) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.5) self.assertEqual(1, self.P.num_datasets()) # failed data should not increase num_datasets try: - self.P.read_data(datafile("badNi.dat")) + self.P.read_data(self.datafile("badNi.dat")) except (RuntimeError, TypeError, NameError, ValueError, IOError): pass self.assertEqual(1, self.P.num_datasets()) # alloc should increase number of datasets # alloc requires a loaded structure - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 30.0, 0.05, 2, 10, 100) self.assertEqual(2, self.P.num_datasets()) self.P.reset() @@ -306,10 +312,10 @@ def test_getcrw(self): self.assertEqual(0, self.P.num_datasets()) # Setting qmax=0 so that partial crw are not disturbed by # termination ripples. - self.P.read_data(datafile("Ni.dat"), "X", 0.0, 0.0) + self.P.read_data(self.datafile("Ni.dat"), "X", 0.0, 0.0) # crw is empty before data refinement self.assertEqual([], self.P.getcrw()) - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.pdfrange(1, 2, 19) self.P.refine() crw19 = numpy.array(self.P.getcrw()) @@ -334,11 +340,11 @@ def test_getcrw(self): def test_getcrw_two_datasets(self): """check that getcrw() and getrw() are consistent for two datasets.""" - self.P.read_data(datafile("Ni.dat"), "X", 25.0, 0.0) + self.P.read_data(self.datafile("Ni.dat"), "X", 25.0, 0.0) self.P.pdfrange(1, 2, 8) - self.P.read_data(datafile("300K.gr"), "N", 32.0, 0.0) + self.P.read_data(self.datafile("300K.gr"), "N", 32.0, 0.0) self.P.pdfrange(2, 1, 11) - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) # mess lattice parameters to have comparable Rw contributions self.P.setvar("lat(1)", 3) self.P.setvar("lat(2)", 3) @@ -389,18 +395,18 @@ def doalloc(): self.assertRaises(pdffit2.unassignedError, self.P.psel, 0) self.assertRaises(pdffit2.unassignedError, self.P.psel, 1) - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) doalloc() self.P.calc() G1 = self.P.getpdf_fit() self.P.reset() - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) doalloc() self.P.calc() G2 = self.P.getpdf_fit() self.P.reset() - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) doalloc() self.P.pdesel("ALL") self.P.psel(1) @@ -429,18 +435,18 @@ def doalloc(): self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 0) self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 1) - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) doalloc() self.P.calc() G1 = self.P.getpdf_fit() self.P.reset() - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) doalloc() self.P.calc() G2 = self.P.getpdf_fit() self.P.reset() - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) doalloc() self.P.psel("ALL") self.P.pdesel(2) @@ -482,7 +488,7 @@ def doalloc(): def test_bond_angle(self): """check PdfFit.bond_angle()""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) a, e = self.P.bond_angle(1, 2, 3) self.assertAlmostEqual(60.0, a, self.places) self.assertRaises(ValueError, self.P.bond_angle, 0, 1, 2) @@ -491,16 +497,16 @@ def test_bond_angle(self): def test_bang(self): "check PdfFit.bang() function" - self.P.read_struct(datafile("Ni.stru")) - out = capture_output(self.P.bang, 1, 2, 3).strip() + self.P.read_struct(self.datafile("Ni.stru")) + out = self.capture_output(self.P.bang, 1, 2, 3).strip() self.assertTrue(out.endswith("60 degrees")) self.assertTrue(out.startswith("NI (#1) - NI (#2) - NI (#3)")) return def test_bond_length_atoms(self): """check PdfFit.bond_length_atoms()""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) dij, ddij = self.P.bond_length_atoms(1, 5) self.assertAlmostEqual(4.03635, dij, self.places) self.P.setphase(1) @@ -509,8 +515,8 @@ def test_bond_length_atoms(self): def test_bond_length_types(self): """check PdfFit.bond_length_types()""" - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) dPbO = self.P.bond_length_types("Pb", "O", 0.1, 3.0) # check if keys are present self.assertTrue("dij" in dPbO) @@ -551,19 +557,19 @@ def test_bond_length_types(self): def test_blen(self): """check PdfFit.blen()""" - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) blen = self.P.blen - o = capture_output(blen, 1, 5).strip() + o = self.capture_output(blen, 1, 5).strip() self.assertTrue(o.endswith("4.03635 A")) self.assertTrue("PB (#1)" in o) self.assertTrue("PB (#5)" in o) self.assertRaises(ValueError, blen, 1, 99) self.assertRaises(ValueError, blen, 0, 1) - o1 = capture_output(blen, 1, 1, 0.1, 1) + o1 = self.capture_output(blen, 1, 1, 0.1, 1) self.assertTrue("No pairs found" in o1) - o2 = capture_output(blen, 1, 50, 0.1, 1) + o2 = self.capture_output(blen, 1, 50, 0.1, 1) self.assertEqual("", o2) - o3 = capture_output(blen, "Sc", "O", 0.5, 2.3).strip() + o3 = self.capture_output(blen, "Sc", "O", 0.5, 2.3).strip() self.assertEqual(1 + 48, len(o3.split("\n"))) self.assertEqual(6, o3.count("SC (#33)")) self.assertEqual(2, o3.count("O (#9)")) @@ -604,11 +610,11 @@ def test_set_scat(self): # check if it is local to phase fPb = self.P.get_scat("X", "Pb") bPb = self.P.get_scat("N", "Pb") - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.set_scat("X", "Pb", 142) self.assertEqual(142, self.P.get_scat("X", "Pb")) self.assertEqual(bPb, self.P.get_scat("N", "Pb")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.assertEqual(fPb, self.P.get_scat("X", "Pb")) self.P.setphase(1) self.assertEqual(142, self.P.get_scat("X", "Pb")) @@ -626,9 +632,9 @@ def test_reset_scat(self): # check if it is local to phase fPb = self.P.get_scat("X", "Pb") bPb = self.P.get_scat("N", "Pb") - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.set_scat("X", "Pb", 142) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.set_scat("N", "Pb", -17) self.P.setphase(1) self.assertNotEqual(fPb, self.P.get_scat("X", "Pb")) @@ -644,9 +650,9 @@ def test_reset_scat(self): def test_num_atoms(self): """check PdfFit.num_atoms()""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertEqual(4, self.P.num_atoms()) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.assertEqual(56, self.P.num_atoms()) self.P.setphase(1) self.assertEqual(4, self.P.num_atoms()) @@ -657,7 +663,7 @@ def test_num_atoms(self): def test_lat(self): """check PdfFit.lat()""" pf = self.P - pf.read_struct(datafile("Ni.stru")) + pf.read_struct(self.datafile("Ni.stru")) for i in ("a", "b", "c", 1, 2, 3): self.assertEqual(3.52, pf.getvar(pf.lat(i))) for i in ("alpha", "beta", "gamma", 4, 5, 6): @@ -667,7 +673,7 @@ def test_lat(self): def test_xyz(self): """check PdfFit.x() PdfFit.y(), PdfFit.z()""" pf = self.P - pf.read_struct(datafile("Ni.stru")) + pf.read_struct(self.datafile("Ni.stru")) self.assertEqual(0.5, pf.getvar(pf.x(3))) self.assertEqual(0, pf.getvar(pf.y(3))) self.assertEqual(0.5, pf.getvar(pf.z(3))) @@ -675,7 +681,7 @@ def test_xyz(self): def test_uij(self): """check PdfFit.uij()""" - ni = loadStructure(datafile("Ni.stru")) + ni = loadStructure(self.datafile("Ni.stru")) ni[2].anisotropy = True ni[2].U11, ni[2].U22, ni[2].U33 = 1, 2, 3 ni[2].U12, ni[2].U13, ni[2].U23 = 4, 5, 6 @@ -692,7 +698,7 @@ def test_uij(self): def test_occ(self): """check PdfFit.occ()""" pf = self.P - pf.read_struct(datafile("Ni.stru")) + pf.read_struct(self.datafile("Ni.stru")) for i in range(1, 5): self.assertEqual(1, pf.getvar(pf.occ(i))) return diff --git a/src/diffpy/pdffit2/tests/TestPhaseFractions.py b/tests/test_phase_fractions.py similarity index 94% rename from src/diffpy/pdffit2/tests/TestPhaseFractions.py rename to tests/test_phase_fractions.py index afc57c06..d4df1c60 100644 --- a/src/diffpy/pdffit2/tests/TestPhaseFractions.py +++ b/tests/test_phase_fractions.py @@ -5,18 +5,23 @@ import unittest +import pytest + from diffpy.pdffit2 import PdfFit -from diffpy.pdffit2.tests.pdffit2testutils import datafile ############################################################################## class TestPhaseFractions(unittest.TestCase): places = 4 + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.alloc("N", 0.0, 0.05, 0.1, 10, 200) return diff --git a/src/diffpy/pdffit2/tests/TestShapeFactors.py b/tests/test_shape_factors.py similarity index 87% rename from src/diffpy/pdffit2/tests/TestShapeFactors.py rename to tests/test_shape_factors.py index 1eaa9711..dd0a0e9c 100644 --- a/src/diffpy/pdffit2/tests/TestShapeFactors.py +++ b/tests/test_shape_factors.py @@ -7,9 +7,9 @@ import unittest import numpy +import pytest from diffpy.pdffit2 import PdfFit, pdffit2 -from diffpy.pdffit2.tests.pdffit2testutils import datafile def spherefactor(r, d): @@ -31,6 +31,10 @@ class TestSphereEnvelope(unittest.TestCase): places = 6 + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + def setUp(self): self.P = PdfFit() return @@ -41,7 +45,7 @@ def tearDown(self): def test_calculation(self): """check calculation of sphere envelope factor""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.calc() d = 8.0 @@ -59,7 +63,7 @@ def test_refinement(self): """check refinement of sphere envelope factor""" dcheck = 8.0 dstart = 12.0 - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setvar("spdiameter", dcheck) self.P.calc() @@ -69,7 +73,7 @@ def test_refinement(self): Gd8noise[::2] += 0.01 Gd8noise[1::2] -= 0.01 self.P.reset() - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.read_data_lists("X", 0.0, 0.05, list(r), list(Gd8noise)) self.P.constrain("spdiameter", "@8") self.P.setpar(8, dstart) @@ -82,20 +86,20 @@ def test_twophase_calculation(self): """check PDF calculation for 2 phases with different spdiameters""" d1 = 6 d2 = 9 - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setvar("spdiameter", d1) self.P.calc() G1 = numpy.array(self.P.getpdf_fit()) self.P.reset() - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setvar("spdiameter", d2) self.P.calc() G2 = numpy.array(self.P.getpdf_fit()) self.P.reset() - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setphase(1) self.P.setvar("spdiameter", d1) @@ -115,13 +119,13 @@ def test_twophase_refinement(self): dstart1 = 8.2 dcheck2 = 6.0 dstart2 = 5.5 - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setvar("spdiameter", dcheck1) self.P.calc() G1 = numpy.array(self.P.getpdf_fit()) self.P.reset() - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setvar("spdiameter", dcheck2) self.P.calc() @@ -131,8 +135,8 @@ def test_twophase_refinement(self): Gnoise[::2] += 0.01 Gnoise[1::2] -= 0.01 self.P.reset() - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.read_data_lists("X", 0.0, 0.05, list(r), list(Gnoise)) self.P.setphase(1) self.P.constrain("spdiameter", "@11") @@ -152,7 +156,7 @@ def test_spdiameter_io(self): """Check reading and writing of spdiameter from structure file.""" import re - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertEqual(0.0, self.P.getvar("spdiameter")) # engine should not write shape factor when not defined spdnone = self.P.save_struct_string(1) @@ -180,6 +184,10 @@ def test_spdiameter_io(self): ############################################################################## class TestStepCutEnvelope(unittest.TestCase): + @pytest.fixture(autouse=True) + def prepare_fixture(self, datafile): + self.datafile = datafile + places = 6 def setUp(self): @@ -192,7 +200,7 @@ def tearDown(self): def test_stepcut_calculation(self): """check calculation of sphere envelope factor""" - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.calc() stepcut = 8.0 @@ -211,20 +219,20 @@ def test_twophase_stepcut_calculation(self): """check PDF calculation for 2 phases with different spdiameters""" d1 = 6 d2 = 9 - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setvar("stepcut", d1) self.P.calc() G1 = numpy.array(self.P.getpdf_fit()) self.P.reset() - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setvar("stepcut", d2) self.P.calc() G2 = numpy.array(self.P.getpdf_fit()) self.P.reset() - self.P.read_struct(datafile("Ni.stru")) - self.P.read_struct(datafile("PbScW25TiO3.stru")) + self.P.read_struct(self.datafile("Ni.stru")) + self.P.read_struct(self.datafile("PbScW25TiO3.stru")) self.P.alloc("X", 0.0, 0.05, 0.1, 10, 200) self.P.setphase(1) self.P.setvar("stepcut", d1) @@ -244,7 +252,7 @@ def test_stepcut_io(self): """Check reading and writing of stepcut from structure file.""" import re - self.P.read_struct(datafile("Ni.stru")) + self.P.read_struct(self.datafile("Ni.stru")) self.assertEqual(0.0, self.P.getvar("stepcut")) # engine should not write shape factor when not defined sscnone = self.P.save_struct_string(1) diff --git a/src/diffpy/pdffit2/tests/testdata/300K.gr b/tests/testdata/300K.gr similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/300K.gr rename to tests/testdata/300K.gr diff --git a/src/diffpy/pdffit2/tests/testdata/Ni.dat b/tests/testdata/Ni.dat similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/Ni.dat rename to tests/testdata/Ni.dat diff --git a/src/diffpy/pdffit2/tests/testdata/Ni.stru b/tests/testdata/Ni.stru similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/Ni.stru rename to tests/testdata/Ni.stru diff --git a/src/diffpy/pdffit2/tests/testdata/PbScW25TiO3.stru b/tests/testdata/PbScW25TiO3.stru similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/PbScW25TiO3.stru rename to tests/testdata/PbScW25TiO3.stru diff --git a/src/diffpy/pdffit2/tests/testdata/badNi.dat b/tests/testdata/badNi.dat similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/badNi.dat rename to tests/testdata/badNi.dat diff --git a/src/diffpy/pdffit2/tests/testdata/badNi.stru b/tests/testdata/badNi.stru similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/badNi.stru rename to tests/testdata/badNi.stru diff --git a/src/diffpy/pdffit2/tests/testdata/badNiZeroVolume.stru b/tests/testdata/badNiZeroVolume.stru similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/badNiZeroVolume.stru rename to tests/testdata/badNiZeroVolume.stru diff --git a/src/diffpy/pdffit2/tests/testdata/noLattice.stru b/tests/testdata/noLattice.stru similarity index 100% rename from src/diffpy/pdffit2/tests/testdata/noLattice.stru rename to tests/testdata/noLattice.stru