Skip to content

Commit

Permalink
[#469] Run python language tests also with C++ optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Jan 31, 2023
1 parent d9a5358 commit e1567eb
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 34 deletions.
27 changes: 21 additions & 6 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,28 @@ test_python()
local MESSAGE="Zserio Python tests"
echo "STARTING - ${MESSAGE}"

activate_python_virtualenv "${ZSERIO_PROJECT_ROOT}" "${ZSERIO_BUILD_DIR}"
if [ $? -ne 0 ] ; then
return 1
fi

if [[ ${SWITCH_CLEAN} == 1 ]] ; then
rm -rf "${TEST_OUT_DIR}/python"
else
activate_python_virtualenv "${ZSERIO_PROJECT_ROOT}" "${ZSERIO_BUILD_DIR}"
if [ $? -ne 0 ] ; then
return 1
fi

python "${UNPACKED_ZSERIO_RELEASE_DIR}"/runtime_libs/python/zserio_cpp/setup.py build \
--build-base="${TEST_OUT_DIR}/python/zserio_cpp" \
--cpp-runtime-dir="${UNPACKED_ZSERIO_RELEASE_DIR}/runtime_libs/cpp/"
if [ $? -ne 0 ] ; then
stderr_echo "Failed to build C++ runtime binding to Python!"
return 1
fi
local ZSERIO_CPP_DIR
ZSERIO_CPP_DIR=$(ls -d1 "${TEST_OUT_DIR}/python/zserio_cpp/lib"*)
if [ $? -ne 0 ] ; then
stderr_echo "Failed to locate C++ runtime binding to Python!"
return 1
fi

local TEST_FILTER=""
for i in ${!TEST_SUITES[@]} ; do
if [ $i -gt 0 ] ; then
Expand All @@ -183,7 +197,8 @@ test_python()
echo

python "${TEST_FILE}" "${TEST_ARGS[@]}" --pylint_rcfile="${PYLINT_RCFILE}" \
--pylint_rcfile_test="${PYLINT_RCFILE_FOR_TESTS}" --mypy_config_file="${MYPY_CONFIG_FILE}"
--pylint_rcfile_test="${PYLINT_RCFILE_FOR_TESTS}" --mypy_config_file="${MYPY_CONFIG_FILE}" \
--zserio_cpp_dir="${ZSERIO_CPP_DIR}"
local PYTHON_RESULT=$?
if [ ${PYTHON_RESULT} -ne 0 ] ; then
stderr_echo "Running python failed with return code ${PYTHON_RESULT}!"
Expand Down
4 changes: 2 additions & 2 deletions test/language/choice_types/python/EmptyChoiceTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def testSelectorConstructor(self):

def testFromReader(self):
selector = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyChoice = self.api.EmptyChoice.from_reader(reader, selector)
self.assertEqual(selector, emptyChoice.selector)
self.assertEqual(0, emptyChoice.bitsizeof())
Expand Down Expand Up @@ -59,7 +59,7 @@ def testInitializeOffsets(self):

def testRead(self):
selector = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyChoice = self.api.EmptyChoice(selector)
emptyChoice.read(reader)
self.assertEqual(selector, emptyChoice.selector)
Expand Down
4 changes: 2 additions & 2 deletions test/language/choice_types/python/EmptyChoiceWithCaseTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def testSelectorConstructor(self):

def testFromReader(self):
selector = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyChoiceWithCase = self.api.EmptyChoiceWithCase.from_reader(reader, selector)
self.assertEqual(selector, emptyChoiceWithCase.selector)
self.assertEqual(0, emptyChoiceWithCase.bitsizeof())
Expand Down Expand Up @@ -61,7 +61,7 @@ def testInitializeOffsets(self):

def testRead(self):
selector = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyChoiceWithCase = self.api.EmptyChoiceWithCase(selector)
emptyChoiceWithCase.read(reader)
self.assertEqual(selector, emptyChoiceWithCase.selector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def testSelectorConstructor(self):

def testFromReader(self):
selector = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyChoiceWithDefault = self.api.EmptyChoiceWithDefault.from_reader(reader, selector)
self.assertEqual(selector, emptyChoiceWithDefault.selector)
self.assertEqual(0, emptyChoiceWithDefault.bitsizeof())
Expand Down Expand Up @@ -61,7 +61,7 @@ def testInitializeOffsets(self):

def testRead(self):
selector = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyChoiceWithDefault = self.api.EmptyChoiceWithDefault(selector)
emptyChoiceWithDefault.read(reader)
self.assertEqual(selector, emptyChoiceWithDefault.selector)
Expand Down
4 changes: 2 additions & 2 deletions test/language/structure_types/python/EmptyStructureTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setUpClass(cls):
cls.api = getZserioApi(__file__, "structure_types.zs").empty_structure

def testFromReader(self):
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyStructure = self.api.EmptyStructure.from_reader(reader)
self.assertEqual(0, emptyStructure.bitsizeof())

Expand Down Expand Up @@ -38,7 +38,7 @@ def testInitializeOffsets(self):
self.assertEqual(bitPosition, emptyStructure.initialize_offsets(bitPosition))

def testRead(self):
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyStructure = self.api.EmptyStructure()
emptyStructure.read(reader)
self.assertEqual(0, emptyStructure.bitsizeof())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def testParamConstructor(self):

def testFromReader(self):
param = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyStructureWithParameter = self.api.EmptyStructureWithParameter.from_reader(reader, param)
self.assertEqual(param, emptyStructureWithParameter.param)
self.assertEqual(0, emptyStructureWithParameter.bitsizeof())
Expand Down Expand Up @@ -54,7 +54,7 @@ def testInitializeOffsets(self):

def testRead(self):
param = 1
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyStructureWithParameter = self.api.EmptyStructureWithParameter(param)
emptyStructureWithParameter.read(reader)
self.assertEqual(param, emptyStructureWithParameter.param)
Expand Down
4 changes: 2 additions & 2 deletions test/language/union_types/python/EmptyUnionTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def testEmptyConstructor(self):
self.assertEqual(0, emptyUnion.bitsizeof())

def testFromReader(self):
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyUnion = self.api.EmptyUnion.from_reader(reader)
self.assertEqual(0, emptyUnion.bitsizeof())

Expand Down Expand Up @@ -47,7 +47,7 @@ def testInitializeOffsets(self):

def testRead(self):
emptyUnion = self.api.EmptyUnion()
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyUnion.read(reader)
self.assertEqual(0, emptyUnion.bitsizeof())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def testParamConstructor(self):
self.assertEqual(self.PARAM_VALUE1, emptyUnionWithParameter.param)

def testFromReader(self):
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyUnionWithParameter = self.api.EmptyUnionWithParameter.from_reader(reader, self.PARAM_VALUE1)
self.assertEqual(self.PARAM_VALUE1, emptyUnionWithParameter.param)
self.assertEqual(0, emptyUnionWithParameter.bitsizeof())
Expand Down Expand Up @@ -53,7 +53,7 @@ def testInitializeOffsets(self):

def testRead(self):
emptyUnionWithParameter = self.api.EmptyUnionWithParameter(self.PARAM_VALUE1)
reader = zserio.BitStreamReader([])
reader = zserio.BitStreamReader(bytes())
emptyUnionWithParameter.read(reader)
self.assertEqual(self.PARAM_VALUE1, emptyUnionWithParameter.param)
self.assertEqual(0, emptyUnionWithParameter.bitsizeof())
Expand Down
44 changes: 30 additions & 14 deletions test/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import argparse
import glob
import pylint.lint
from multiprocessing import Process

def main():
testRoot = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -26,6 +27,7 @@ def main():
argParser.add_argument("--pylint_rcfile")
argParser.add_argument("--pylint_rcfile_test")
argParser.add_argument("--mypy_config_file")
argParser.add_argument("--zserio_cpp_dir")
argParser.set_defaults(filter="**", verbosity=2)
args = argParser.parse_args()
if args.build_dir:
Expand All @@ -38,8 +40,7 @@ def main():
# path to zserio runtime release
runtimePath = os.path.join(TEST_ARGS["release_dir"], "runtime_libs", "python")
sys.path.append(runtimePath)

sysPathBeforeTests = list(sys.path)
sys.path.append(args.zserio_cpp_dir)

# detect test directories
testPattern = "*Test.py"
Expand All @@ -60,20 +61,17 @@ def main():
# sort test dirs
testDirs = sorted(testDirs)

# load tests
loader = unittest.TestLoader()
testSuite = unittest.TestSuite()
for testDir in testDirs:
loadedTests = loader.discover(testDir, pattern=testPattern, top_level_dir=testDir)
testSuite.addTest(loadedTests)

runner = unittest.TextTestRunner(verbosity=args.verbosity)
testResult = runner.run(testSuite)
if not testResult.wasSuccessful():
# run tests with pure python runtime
print("\nRunning python language tests with pure python runtime.")
os.environ["ZSERIO_PYTHON_IMPLEMENTATION"] = "python"
if not _runTests(args, testDirs, testPattern) :
return 1

# restore sys.path to get rid of what test runner recently added
sys.path = sysPathBeforeTests
# run tests with python runtime and optimized zserio_cpp
print("\nRunning python language tests with C++ optimized runtime.")
os.environ["ZSERIO_PYTHON_IMPLEMENTATION"] = "cpp"
if not _runTests(args, testDirs, testPattern):
return 1

# run pylint
pylintResult = _runPylintOnAllSources(args, testDirs)
Expand All @@ -82,6 +80,24 @@ def main():

return _runMypyOnAllSources(args, testDirs, runtimePath, testutilsPath)

def _runTests(args, testDirs, testPattern):
p = Process(target=_runTestsProcess, args=(args, testDirs, testPattern))
p.start()
p.join()
return p.exitcode == 0

def _runTestsProcess(args, testDirs, testPattern):
loader = unittest.TestLoader()
testSuite = unittest.TestSuite()
for testDir in testDirs:
loadedTests = loader.discover(testDir, pattern=testPattern, top_level_dir=testDir)
testSuite.addTest(loadedTests)

runner = unittest.TextTestRunner(verbosity=args.verbosity)
testResult = runner.run(testSuite)

sys.exit(0 if testResult.wasSuccessful() else 1)

def _runPylintOnAllSources(args, testDirs):
print("\nRunning pylint on python tests")

Expand Down

0 comments on commit e1567eb

Please sign in to comment.