From b8b17e00f04f23f0cc2f6e940e804f6bd30812d7 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Sat, 22 Jun 2019 20:36:42 +0200 Subject: Switch from nosetests to Python unittests Signed-off-by: Michael Buesch --- tests/__init__.py | 2 ++ tests/pyprofibus_tstlib.py | 18 +++++----- tests/run.sh | 69 ++++++++++++++++------------------- tests/test_dummy.py | 90 ++++++++++++++++++++++++---------------------- tests/test_gsd.py | 22 +++++++----- 5 files changed, 102 insertions(+), 99 deletions(-) create mode 100644 tests/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..eef728f --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,2 @@ +from test_dummy import * +from test_gsd import * diff --git a/tests/pyprofibus_tstlib.py b/tests/pyprofibus_tstlib.py index 7175e92..2ddb214 100644 --- a/tests/pyprofibus_tstlib.py +++ b/tests/pyprofibus_tstlib.py @@ -1,13 +1,13 @@ from __future__ import division, absolute_import, print_function, unicode_literals -import pyprofibus -import pyprofibus.phy_dummy -import pyprofibus.phy_serial +from unittest import TestCase -import nose as __nose +__all__ = [ + "TestCase", + "initTest", +] -assert_eq = __nose.tools.assert_equal -assert_lt = __nose.tools.assert_less -assert_le = __nose.tools.assert_less_equal -assert_gt = __nose.tools.assert_greater -assert_ge = __nose.tools.assert_greater_equal + +def initTest(testCaseFile): + from os.path import basename + print("(test case file: %s)" % basename(testCaseFile)) diff --git a/tests/run.sh b/tests/run.sh index b689285..7e865f6 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -10,39 +10,27 @@ rootdir="$basedir/.." die() { - echo "$*" + [ -n "$*" ] && echo "$*" >&2 exit 1 } -# $1=executable_name -find_executable() -{ - local executable_name="$1" - - local executable_path="$(which "$executable_name")" - [ -n "$executable_path" ] ||\ - die "$executable_name executable not found."\ - "Please install $executable_name." - RET="$executable_path" -} - # $1=interpreter -# $2=nose -# $3=test_dir -run_nose() +# $2=test_dir +run_pyunit() { local interpreter="$1" - local nose="$2" - local test_dir="$3" + local test_dir="$2" - echo - echo "===" - echo "= Running $interpreter $nose..." - echo "===" - export PYTHONPATH="$rootdir/tests:$PYTHONPATH" - cd "$rootdir" || die "Failed to cd to rootdir." - "$interpreter" "$nose" -v --no-byte-compile "$test_dir" ||\ - die "Test failed" + ( + echo + echo "===" + echo "= Running $interpreter..." + echo "===" + export PYTHONPATH="$rootdir/tests:$PYTHONPATH" + cd "$rootdir" || die "Failed to cd to rootdir." + "$interpreter" -m unittest --failfast --buffer --catch "$test_dir" ||\ + die "Test failed" + ) || die } # $1=test_dir @@ -50,20 +38,23 @@ run_testdir() { local test_dir="$1" - find_executable nosetests - local nosetests="$RET" - find_executable nosetests3 - local nosetests3="$RET" - - export PYTHONPATH= - run_nose python2 "$nosetests" "$test_dir" - - export PYTHONPATH= - run_nose python3 "$nosetests3" "$test_dir" + unset PYTHONPATH + unset PYTHONSTARTUP + unset PYTHONY2K + unset PYTHONOPTIMIZE + unset PYTHONDEBUG + export PYTHONDONTWRITEBYTECODE=1 + unset PYTHONINSPECT + unset PYTHONIOENCODING + unset PYTHONNOUSERSITE + unset PYTHONUNBUFFERED + unset PYTHONVERBOSE + export PYTHONWARNINGS=once + export PYTHONHASHSEED=random - local p='import sys; print(":".join(p for p in sys.path if p.startswith("/usr/")))' - export PYTHONPATH="$(pypy -c "$p"):$(python2 -c "$p")" - run_nose pypy "$nosetests" "$test_dir" + run_pyunit python2 "$test_dir" + run_pyunit python3 "$test_dir" + run_pyunit pypy "$test_dir" } run_tests() diff --git a/tests/test_dummy.py b/tests/test_dummy.py index 276df99..3d98b42 100644 --- a/tests/test_dummy.py +++ b/tests/test_dummy.py @@ -1,47 +1,53 @@ from __future__ import division, absolute_import, print_function, unicode_literals from pyprofibus_tstlib import * +initTest(__file__) +import pyprofibus +import pyprofibus.phy_dummy +import pyprofibus.phy_serial -def test_dummy_phy(): - phy = pyprofibus.phy_dummy.CpPhyDummySlave(debug = True) - phy.setConfig(baudrate=19200) - - master = pyprofibus.DPM1(phy=phy, - masterAddr=42, - debug=True) - - slaveDesc = pyprofibus.DpSlaveDesc(gsd=None, - slaveAddr=84) - - slaveDesc.setCfgDataElements([ - pyprofibus.DpCfgDataElement(pyprofibus.DpCfgDataElement.ID_TYPE_OUT), - pyprofibus.DpCfgDataElement(pyprofibus.DpCfgDataElement.ID_TYPE_IN), - ]) - - slaveDesc.setUserPrmData(bytearray([1, 2, 3, 4, ])) - - slaveDesc.setSyncMode(True) - slaveDesc.setFreezeMode(True) - slaveDesc.setGroupMask(1) - slaveDesc.setWatchdog(300) - - master.addSlave(slaveDesc) - master.initialize() - - # Run slave initialization state machine. - for i in range(25): - slaveDesc.setOutData(bytearray([1, ])) - master.run() - # Check dummy-slave response to Data_Exchange. - for i in range(100): - print("testing %d" % i) - j = 0 - while True: - j += 1 - assert_lt(j, 10) - slaveDesc.setOutData(bytearray([i, ])) + +class Test_DummyPhy(TestCase): + def test_dummy_phy(self): + phy = pyprofibus.phy_dummy.CpPhyDummySlave(debug=True) + phy.setConfig(baudrate=19200) + + master = pyprofibus.DPM1(phy=phy, + masterAddr=42, + debug=True) + + slaveDesc = pyprofibus.DpSlaveDesc(gsd=None, + slaveAddr=84) + + slaveDesc.setCfgDataElements([ + pyprofibus.DpCfgDataElement(pyprofibus.DpCfgDataElement.ID_TYPE_OUT), + pyprofibus.DpCfgDataElement(pyprofibus.DpCfgDataElement.ID_TYPE_IN), + ]) + + slaveDesc.setUserPrmData(bytearray([1, 2, 3, 4, ])) + + slaveDesc.setSyncMode(True) + slaveDesc.setFreezeMode(True) + slaveDesc.setGroupMask(1) + slaveDesc.setWatchdog(300) + + master.addSlave(slaveDesc) + master.initialize() + + # Run slave initialization state machine. + for i in range(25): + slaveDesc.setOutData(bytearray([1, ])) master.run() - ret = slaveDesc.getInData() - if j >= 5 and ret is not None: - break - assert_eq(bytearray(ret), bytearray([i ^ 0xFF, ])) + # Check dummy-slave response to Data_Exchange. + for i in range(100): + print("testing %d" % i) + j = 0 + while True: + j += 1 + self.assertTrue(j < 10) + slaveDesc.setOutData(bytearray([i, ])) + master.run() + ret = slaveDesc.getInData() + if j >= 5 and ret is not None: + break + self.assertEqual(bytearray(ret), bytearray([i ^ 0xFF, ])) diff --git a/tests/test_gsd.py b/tests/test_gsd.py index 3bf54d0..1fc58c8 100644 --- a/tests/test_gsd.py +++ b/tests/test_gsd.py @@ -1,14 +1,18 @@ from __future__ import division, absolute_import, print_function, unicode_literals from pyprofibus_tstlib import * +initTest(__file__) +import pyprofibus -def test_gsdinterp(): - gsd = pyprofibus.GsdInterp.fromFile("dummy.gsd") - gsd.setConfiguredModule("dummy input module") - gsd.setConfiguredModule("dummy output module") - assert_eq([ e.getDU() - for e in gsd.getCfgDataElements() ], - [ [0x00, ], [0x10, ], [0x20, ], ]) - assert_eq(gsd.getIdentNumber(), 0x4224) - assert_eq(gsd.getUserPrmData(), bytearray([0x00, 0x00, 0x00, 0x42])) +class Test_GSD(TestCase): + def test_gsdinterp(self): + gsd = pyprofibus.GsdInterp.fromFile("dummy.gsd") + gsd.setConfiguredModule("dummy input module") + gsd.setConfiguredModule("dummy output module") + + self.assertEqual([ e.getDU() + for e in gsd.getCfgDataElements() ], + [ [0x00, ], [0x10, ], [0x20, ], ]) + self.assertEqual(gsd.getIdentNumber(), 0x4224) + self.assertEqual(gsd.getUserPrmData(), bytearray([0x00, 0x00, 0x00, 0x42])) -- cgit v1.2.3