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:
Håkon Hægland 2020-11-04 13:43:51 +01:00
parent 648bab7108
commit 7011a6c0b7
4 changed files with 26 additions and 20 deletions

View File

@ -25,7 +25,6 @@
#include <opm/models/utils/propertysystem.hh>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/embed.h>
namespace py = pybind11;

View File

@ -1,3 +0,0 @@
# !/bin/bash
@PYTHON_EXECUTABLE@ -m unittest

View File

@ -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)
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
DESTINATION ${PROJECT_BINARY_DIR}/python)
file( COPY ${PROJECT_SOURCE_DIR}/python/test_data
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
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PROJECT_BINARY_DIR}/python
${BASH_EXECUTABLE} run-python-tests.sh )
endif(BASH_EXECUTABLE)
COMMAND ${CMAKE_COMMAND}
-E env PYTHONPATH=${PROJECT_BINARY_DIR}/python:$ENV{PYTHONPATH}
${PYTHON_EXECUTABLE} -m unittest )

View File

@ -1,11 +1,12 @@
import unittest
from opm2.simulators import BlackOilSimulator
import os
import unittest
from contextlib import contextmanager
from pathlib import Path
from opm2.simulators import BlackOilSimulator
class TestBasic(unittest.TestCase):
@classmethod
def setUpClass(cls):
os.chdir('test_data/SPE1CASE1')
# NOTE: Loading the below extension module involves loading a
# a shared library like "simulators.cpython-37m-x86_64-linux-gnu.so"
# 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).
# However, as noted above this is not currently possible.
#
cls.sim = BlackOilSimulator( 'SPE1CASE1.DATA' )
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' )
def setUp(self):
self.saved_cwd = os.getcwd()
os.chdir(self.data_dir)
self.sim.step_init()
self.sim.step()
def tearDown(self):
self.sim.step_cleanup()
os.chdir(self.saved_cwd)
def test_all(self):
sim = self.sim
@ -61,4 +68,14 @@ class TestBasic(unittest.TestCase):
sim.set_porosity(poro)
sim.step()
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)