forked from Matgenix/turbomoleio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
168 lines (134 loc) · 5.35 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
# The turbomoleio package, a python interface to Turbomole
# for preparing inputs, parsing outputs and other related tools.
#
# Copyright (C) 2018-2022 BASF SE, Matgenix SRL.
#
# This file is part of turbomoleio.
#
# Turbomoleio is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Turbomoleio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with turbomoleio (see ~turbomoleio/COPYING). If not,
# see <https://www.gnu.org/licenses/>.
"""Configuration, fixtures and utilities for pytest testing infrastructure."""
import os
import pytest
from pymatgen.core.structure import Molecule, Structure
from turbomoleio.core.control import Control
TESTDIR = os.path.join(os.path.split(__file__)[0], "turbomoleio", "testfiles")
DRYRUN_FPATH = os.path.join(os.path.split(__file__)[0], "dryrun_itest.json")
@pytest.fixture(autouse=True, scope="session")
def testdir():
"""Get test files directory."""
return TESTDIR
@pytest.fixture
def control_filepath(control_filename):
"""Get path to control file from file name."""
return os.path.join(TESTDIR, "control", control_filename)
@pytest.fixture
def control(control_filename):
"""Get Control object from file name."""
return Control.from_file(os.path.join(TESTDIR, "control", control_filename))
@pytest.fixture
def molecule_filepath(molecule_filename):
"""Get path to molecule file."""
return os.path.join(TESTDIR, "structures", molecule_filename)
@pytest.fixture
def molecule(molecule_filename):
"""Get Molecule object from file name."""
return Molecule.from_file(os.path.join(TESTDIR, "structures", molecule_filename))
@pytest.fixture
def structure(structure_filename):
"""Get Structure object from file name."""
return Structure.from_file(os.path.join(TESTDIR, "structures", structure_filename))
@pytest.fixture
def structure_filepath(structure_filename):
"""Get path to structure file."""
return os.path.join(TESTDIR, "structures", structure_filename)
@pytest.fixture
def mo_dirpath(dir_name):
"""Get molecular orbital directory path from directory name."""
return os.path.join(TESTDIR, "mo", dir_name)
@pytest.fixture(scope="session")
def delete_tmp_dir(request):
"""Get option whether to delete the temporary directory or not."""
return not request.config.getoption("--keep-tmpdir", False)
def pytest_configure(config):
"""Configure pytest."""
from turbomoleio.testfiles.utils import ItestConfig
ItestConfig.define_timeout = config.getoption("--define-timeout")
ItestConfig.generate_ref = config.getoption("--generate-itest-ref")
ItestConfig.dryrun = config.getoption("--dryrun-itest")
dryrun_fpath = config.getoption("--dryrun-fpath")
ItestConfig.dryrun_fpath = os.path.join(os.path.split(__file__)[0], dryrun_fpath)
ItestConfig.dryrun_use_ref_control = config.getoption(
"--dryrun-use-reference-control"
)
ItestConfig.tol = config.getoption("--itest-tol")
ItestConfig.delete_tmp_dir = not config.getoption("--keep-tmpdir")
# When running in dry mode, only the integration tests should be run and
# there should not be any failures.
if ItestConfig.dryrun:
config.option.markexpr = "integration"
config.option.maxfail = 1
def pytest_addoption(parser):
"""Add options to pytest."""
parser.addoption(
"--keep-tmpdir",
action="store_true",
default=False,
help="Temporary directories used during the tests will not be deleted.",
)
parser.addoption(
"--define-timeout",
default=10,
type=int,
help="The number of seconds used for the timeout in DefineRunner "
"for integration tests.",
)
parser.addoption(
"--generate-itest-ref",
action="store_true",
default=False,
help="The output control file generated during the integration "
"tests will be copied to the reference folder. "
"N.B. this will overwrite previously existing files.",
)
parser.addoption(
"--dryrun-itest",
action="store_true",
default=False,
help="The files generated during the integration tests will be compared "
"against reference files. A list of all differences will be "
"provided in a file.",
)
parser.addoption(
"--dryrun-use-reference-control",
action="store_true",
default=False,
help="Use the reference control file for integration tests "
"instead of the one generated using define. "
"The control file is still generated and compared.",
)
parser.addoption(
"--dryrun-fpath",
default=DRYRUN_FPATH,
type=str,
help="Filepath for the results of the differences of the dryrun.",
)
parser.addoption(
"--itest-tol",
default=1e-4,
type=float,
help="The absolute tolerance used in the integration test to match "
"floating point numbers when comparing to reference files.",
)