Skip to content

Commit 18f4e25

Browse files
authored
Merge pull request #5894 from brockdyer03/ntest-pytest
Nexus: Switch to Pytest
2 parents 6b0609a + caf9bb7 commit 18f4e25

63 files changed

Lines changed: 206 additions & 41 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMake/python.cmake

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ function(TEST_PYTHON_MODULE MODULE_NAME MODULE_PRESENT)
1414
CACHE BOOL "" FORCE)
1515
endfunction()
1616

17+
# Ensure we have a compatible version of Pytest for Nexus's testing.
18+
function(CHECK_PYTEST_VERSION pytest_version_ok)
19+
execute_process(COMMAND ${Python3_EXECUTABLE} -m pytest --version
20+
RESULT_VARIABLE PYTEST_VERSION_RESULT
21+
OUTPUT_VARIABLE PYTEST_VERSION
22+
ERROR_VARIABLE PYTEST_VERSION_ERROR
23+
OUTPUT_STRIP_TRAILING_WHITESPACE)
24+
if(PYTEST_VERSION)
25+
string(REPLACE "pytest " "" PYTEST_VERSION ${PYTEST_VERSION})
26+
else()
27+
# Old versions of pytest put the version output in stderr for some reason
28+
string(REPLACE "pytest " "" PYTEST_VERSION ${PYTEST_VERSION_ERROR})
29+
endif()
30+
if(PYTEST_VERSION VERSION_GREATER_EQUAL "6.2.4")
31+
message(STATUS "Found compatible Pytest version ${PYTEST_VERSION}")
32+
set(${pytest_version_ok}
33+
TRUE
34+
CACHE BOOL "" FORCE)
35+
else()
36+
message(STATUS "Incompatible Pytest version ${PYTEST_VERSION} found, tests will not be added. Required version >= 6.2.4 ")
37+
set(${pytest_version_ok}
38+
FALSE
39+
CACHE BOOL "" FORCE)
40+
endif()
41+
endfunction()
42+
1743
# Test python module prerequisites for a particular test script
1844
# module_list - input - list of module names, for example "numpy;h5py" (including the quotation marks)
1945
# test_name - input - name of test (used for missing module message)
@@ -40,6 +66,17 @@ function(CHECK_PYTHON_REQS module_list test_name add_test)
4066
set(${add_test}
4167
false
4268
PARENT_SCOPE)
69+
else()
70+
if(${python_module} STREQUAL "pytest")
71+
check_pytest_version(pytest_version_ok)
72+
if(NOT ${pytest_version_ok})
73+
message("Pytest version < 6.2.4 found, will not add Nexus tests")
74+
set(${add_test}
75+
false
76+
PARENT_SCOPE)
77+
endif()
78+
endif()
4379
endif()
4480
endforeach()
4581
endfunction()
82+

nexus/nexus/tests/CMakeLists.txt

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,63 @@
22
include("${PROJECT_SOURCE_DIR}/CMake/test_labels.cmake")
33
include("${PROJECT_SOURCE_DIR}/CMake/python.cmake")
44

5-
check_python_reqs(numpy "nexus base" ADD_TEST)
5+
check_python_reqs("numpy;pytest" "nexus base" ADD_TEST)
66

77
if(ADD_TEST)
8-
file(TIMESTAMP ${qmcpack_SOURCE_DIR}/nexus/nexus/bin/nxs-test NEXUS_TESTLIST_TIMESTAMP)
9-
if(NOT "${NEXUS_TESTLIST_TIMESTAMP}" STREQUAL "${CACHE_NEXUS_TESTLIST_TIMESTAMP}")
10-
message("Generating Nexus test list for the first time or after a nxs-test time stamp change")
11-
execute_process(COMMAND ${Python3_EXECUTABLE} ${qmcpack_SOURCE_DIR}/nexus/nexus/bin/nxs-test --ctestlist
12-
RESULT_VARIABLE NEXUS_TESTLIST_RESULT OUTPUT_VARIABLE NEXUS_TESTLIST)
13-
if(NOT NEXUS_TESTLIST_RESULT EQUAL 0)
14-
message(FATAL_ERROR "Command line '${Python3_EXECUTABLE} ${qmcpack_SOURCE_DIR}/nexus/nexus/bin/nxs-test --ctestlist' failed!")
15-
endif()
16-
17-
set(CACHE_NEXUS_TESTLIST
18-
${NEXUS_TESTLIST}
19-
CACHE INTERNAL "NEXUS_TESTLIST cache variable" FORCE)
20-
set(CACHE_NEXUS_TESTLIST_TIMESTAMP
21-
${NEXUS_TESTLIST_TIMESTAMP}
22-
CACHE INTERNAL "Timestamp used to validate NEXUS_TESTLIST cache variables" FORCE)
23-
else()
24-
message("nxs-test time stamp remains unchanged. Using cached Nexus test list")
25-
set(NEXUS_TESTLIST ${CACHE_NEXUS_TESTLIST})
8+
message("Generating Nexus test list")
9+
execute_process(COMMAND ${Python3_EXECUTABLE} -m pytest --collect-only -qq --disable-warnings ${qmcpack_SOURCE_DIR}/nexus/nexus/tests
10+
RESULT_VARIABLE NEXUS_TESTLIST_RESULT OUTPUT_VARIABLE NEXUS_TESTLIST)
11+
if(NOT NEXUS_TESTLIST_RESULT EQUAL 0)
12+
message(FATAL_ERROR "Command line ' ${Python3_EXECUTABLE} -m pytest --collect-only -qq ${qmcpack_SOURCE_DIR}/nexus/nexus/tests ' failed!")
2613
endif()
14+
15+
string(REPLACE "\n" ";" NEXUS_TESTLIST "${NEXUS_TESTLIST}") # Join into a single line with semicolon separators
16+
list(REMOVE_ITEM NEXUS_TESTLIST "") # remove empty entries
17+
set(CACHE_NEXUS_TESTLIST
18+
${NEXUS_TESTLIST}
19+
CACHE INTERNAL "NEXUS_TESTLIST cache variable" FORCE)
2720
if(NEXUS_TESTLIST)
2821
message("Adding Nexus tests")
2922
else()
3023
message("Nexus test list is empty. No Nexus tests added.")
3124
endif()
32-
foreach(TESTNAME ${NEXUS_TESTLIST})
33-
#message("Adding test ntest_nexus_${TESTNAME}")
34-
set(NTEST "${qmcpack_SOURCE_DIR}/nexus/nexus/bin/nxs-test")
35-
IF(ENABLE_PYCOV)
36-
add_test(NAME ntest_nexus_${TESTNAME} COMMAND ${PYTHON_COVERAGE_EXECUTABLE} run -p ${NTEST} -R ${TESTNAME}\$ --ctest
37-
--pythonpath=${PROJECT_SOURCE_DIR}/nexus:$ENV{PYTHONPATH})
38-
39-
ELSE()
40-
add_test(NAME ntest_nexus_${TESTNAME} COMMAND ${Python3_EXECUTABLE} ${NTEST} -R ${TESTNAME}\$ --ctest
41-
--pythonpath=${PROJECT_SOURCE_DIR}/nexus:$ENV{PYTHONPATH})
42-
ENDIF()
43-
set_tests_properties(ntest_nexus_${TESTNAME} PROPERTIES ENVIRONMENT PYTHONPATH=$ENV{PYTHONPATH})
25+
set(NTEST "pytest")
26+
if(ENABLE_PYCOV)
27+
add_test(NAME ntest_nexus_coverage_all
28+
COMMAND ${Python3_EXECUTABLE} -m ${NTEST} ${qmcpack_SOURCE_DIR}/nexus/nexus/tests -c ${qmcpack_SOURCE_DIR}/nexus/pyproject.toml
29+
--cov=nexus
30+
--cov-report=xml
31+
--cov-config=${qmcpack_SOURCE_DIR}/nexus/pyproject.toml
32+
)
33+
set_tests_properties(ntest_nexus_coverage_all PROPERTIES ENVIRONMENT PYTHONPATH=$ENV{PYTHONPATH})
4434
set_property(
45-
TEST ntest_nexus_${TESTNAME}
35+
TEST ntest_nexus_coverage_all
4636
APPEND
4737
PROPERTY LABELS "nexus;deterministic")
4838
set(TEST_LABELS "")
49-
add_test_labels(ntest_nexus_${TESTNAME} TEST_LABELS)
50-
# Use a resource lock to avoid potential race condition in copying example inputs
51-
if(${TESTNAME} MATCHES ".*example.*")
52-
set_tests_properties(ntest_nexus_${TESTNAME} PROPERTIES RESOURCE_LOCK nexus_examples_resource)
53-
endif()
54-
endforeach()
39+
add_test_labels(ntest_nexus_coverage_all TEST_LABELS)
40+
else()
41+
foreach(TESTFILE IN LISTS NEXUS_TESTLIST)
42+
string(REGEX REPLACE "\.py:.*[0-9]" "" TESTFILE "${TESTFILE}")
43+
string(REPLACE "nexus/tests/test_" "" TESTNAME "${TESTFILE}")
44+
#message("Adding test ntest_nexus_${TESTNAME}")
45+
46+
add_test(NAME ntest_nexus_${TESTNAME}
47+
COMMAND ${Python3_EXECUTABLE} -m ${NTEST} ${qmcpack_SOURCE_DIR}/nexus/${TESTFILE}.py -c ${qmcpack_SOURCE_DIR}/nexus/pyproject.toml
48+
)
49+
set_tests_properties(ntest_nexus_${TESTNAME} PROPERTIES ENVIRONMENT PYTHONPATH=$ENV{PYTHONPATH})
50+
set_property(
51+
TEST ntest_nexus_${TESTNAME}
52+
APPEND
53+
PROPERTY LABELS "nexus;deterministic")
54+
set(TEST_LABELS "")
55+
add_test_labels(ntest_nexus_${TESTNAME} TEST_LABELS)
56+
# Use a resource lock to avoid potential race condition in copying example inputs
57+
if(${TESTNAME} MATCHES ".*example.*")
58+
set_tests_properties(ntest_nexus_${TESTNAME} PROPERTIES RESOURCE_LOCK nexus_examples_resource)
59+
endif()
60+
endforeach()
61+
endif()
5562
else()
56-
message("Skipping Nexus tests because numpy is not present in python installation")
63+
message(WARNING "Skipping Nexus tests because python numpy package and/or pytest were not found")
5764
endif()

nexus/nexus/tests/test_basisset.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.BASISSET)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

nexus/nexus/tests/test_bundle.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.BUNDLE)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

nexus/nexus/tests/test_developer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.DEVELOPER)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

nexus/nexus/tests/test_execute.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.EXECUTE)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

nexus/nexus/tests/test_fileio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.FILEIO)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

nexus/nexus/tests/test_gamess_analyzer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.GAMESS_ANALYZER)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

nexus/nexus/tests/test_gamess_input.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.GAMESS_INPUT)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

nexus/nexus/tests/test_gamess_simulation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from . import NexusTestOrder
44
pytestmark = pytest.mark.order(NexusTestOrder.GAMESS_SIMULATION)
5+
from ..generic import generic_settings
6+
generic_settings.raise_error = True
57
except ImportError:
68
pass
79

0 commit comments

Comments
 (0)