mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
Restore cwd after each unittest.
Restores the original cwd after each unittest in test_basic.py. Also simplifies add_test() in python/simulators/CMakeLists.txt such that the Bash script wrapper run-python-tests.sh is no longer needed to run the tests.
This commit is contained in:
@@ -25,7 +25,6 @@
|
|||||||
#include <opm/models/utils/propertysystem.hh>
|
#include <opm/models/utils/propertysystem.hh>
|
||||||
#include <pybind11/pybind11.h>
|
#include <pybind11/pybind11.h>
|
||||||
#include <pybind11/numpy.h>
|
#include <pybind11/numpy.h>
|
||||||
#include <pybind11/embed.h>
|
|
||||||
|
|
||||||
namespace py = pybind11;
|
namespace py = pybind11;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
# !/bin/bash
|
|
||||||
|
|
||||||
@PYTHON_EXECUTABLE@ -m unittest
|
|
||||||
@@ -20,20 +20,13 @@ set(PYTHON_INSTALL_PREFIX "lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MI
|
|||||||
|
|
||||||
install(TARGETS simulators DESTINATION ${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}/opm)
|
install(TARGETS simulators DESTINATION ${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}/opm)
|
||||||
|
|
||||||
configure_file(${PROJECT_SOURCE_DIR}/python/run-python-tests.sh.in
|
|
||||||
${PROJECT_BINARY_DIR}/tmp/run-python-tests.sh)
|
|
||||||
file( COPY ${PROJECT_BINARY_DIR}/tmp/run-python-tests.sh
|
|
||||||
DESTINATION ${PROJECT_BINARY_DIR}/python
|
|
||||||
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE )
|
|
||||||
file( COPY ${PROJECT_SOURCE_DIR}/python/test
|
file( COPY ${PROJECT_SOURCE_DIR}/python/test
|
||||||
DESTINATION ${PROJECT_BINARY_DIR}/python)
|
DESTINATION ${PROJECT_BINARY_DIR}/python)
|
||||||
file( COPY ${PROJECT_SOURCE_DIR}/python/test_data
|
file( COPY ${PROJECT_SOURCE_DIR}/python/test_data
|
||||||
DESTINATION ${PROJECT_BINARY_DIR}/python)
|
DESTINATION ${PROJECT_BINARY_DIR}/python)
|
||||||
|
|
||||||
find_program (BASH_EXECUTABLE bash)
|
|
||||||
if (BASH_EXECUTABLE)
|
|
||||||
add_test(NAME python_tests
|
add_test(NAME python_tests
|
||||||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python
|
||||||
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PROJECT_BINARY_DIR}/python
|
COMMAND ${CMAKE_COMMAND}
|
||||||
${BASH_EXECUTABLE} run-python-tests.sh )
|
-E env PYTHONPATH=${PROJECT_BINARY_DIR}/python:$ENV{PYTHONPATH}
|
||||||
endif(BASH_EXECUTABLE)
|
${PYTHON_EXECUTABLE} -m unittest )
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import unittest
|
|
||||||
from opm2.simulators import BlackOilSimulator
|
|
||||||
import os
|
import os
|
||||||
|
import unittest
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from pathlib import Path
|
||||||
|
from opm2.simulators import BlackOilSimulator
|
||||||
|
|
||||||
class TestBasic(unittest.TestCase):
|
class TestBasic(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
os.chdir('test_data/SPE1CASE1')
|
|
||||||
# NOTE: Loading the below extension module involves loading a
|
# NOTE: Loading the below extension module involves loading a
|
||||||
# a shared library like "simulators.cpython-37m-x86_64-linux-gnu.so"
|
# a shared library like "simulators.cpython-37m-x86_64-linux-gnu.so"
|
||||||
# It turns out Python cannot unload this module, see:
|
# It turns out Python cannot unload this module, see:
|
||||||
@@ -43,14 +44,20 @@ class TestBasic(unittest.TestCase):
|
|||||||
# in which each test_xxx() method is called by unittest is not defined).
|
# in which each test_xxx() method is called by unittest is not defined).
|
||||||
# However, as noted above this is not currently possible.
|
# However, as noted above this is not currently possible.
|
||||||
#
|
#
|
||||||
|
test_dir = Path(os.path.dirname(__file__))
|
||||||
|
cls.data_dir = test_dir.parent.joinpath("test_data/SPE1CASE1")
|
||||||
|
with TestBasic.pushd(cls.data_dir):
|
||||||
cls.sim = BlackOilSimulator( 'SPE1CASE1.DATA' )
|
cls.sim = BlackOilSimulator( 'SPE1CASE1.DATA' )
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.saved_cwd = os.getcwd()
|
||||||
|
os.chdir(self.data_dir)
|
||||||
self.sim.step_init()
|
self.sim.step_init()
|
||||||
self.sim.step()
|
self.sim.step()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.sim.step_cleanup()
|
self.sim.step_cleanup()
|
||||||
|
os.chdir(self.saved_cwd)
|
||||||
|
|
||||||
def test_all(self):
|
def test_all(self):
|
||||||
sim = self.sim
|
sim = self.sim
|
||||||
@@ -61,4 +68,14 @@ class TestBasic(unittest.TestCase):
|
|||||||
sim.set_porosity(poro)
|
sim.set_porosity(poro)
|
||||||
sim.step()
|
sim.step()
|
||||||
poro2 = sim.get_porosity()
|
poro2 = sim.get_porosity()
|
||||||
self.assertAlmostEqual(poro[0], 0.285, places=7, msg='value of porosity 2')
|
self.assertAlmostEqual(poro2[0], 0.285, places=7, msg='value of porosity 2')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
@contextmanager
|
||||||
|
def pushd(cls, path):
|
||||||
|
cwd = os.getcwd()
|
||||||
|
if not os.path.isdir(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
os.chdir(path)
|
||||||
|
yield
|
||||||
|
os.chdir(cwd)
|
||||||
|
|||||||
Reference in New Issue
Block a user