From b6d19293067fdceec38061018e2bbf8b980a5d95 Mon Sep 17 00:00:00 2001 From: Ryan Flynn Date: Mon, 2 Jan 2017 21:54:08 -0500 Subject: [PATCH] add unit testing via 'make test' --- .travis.yml | 12 +++++++----- Makefile | 10 ++++++++++ labelImg.py | 12 ++++++++++-- tests/test.py | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 tests/test.py diff --git a/.travis.yml b/.travis.yml index f0ea98c0..0b34b92c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ matrix: before_install: - sudo pip install lxml - make qt4py2 - - ( xvfb-run python2.7 labelImg.py ) & sleep 10 ; kill $! + - xvfb-run make testpy2 # Python 2 + QT5 # disabled; can't get it to work @@ -47,7 +47,7 @@ matrix: # - sudo pip install lxml # - pyrcc5 --help || true # does QT5 support python2 out of the box? # - make qt5py3 - # - ( xvfb-run python2.7 labelImg.py ) & sleep 10 ; kill $! + # - xvfb-run make testpy2 # Python 3 + QT4 - os: linux @@ -69,7 +69,7 @@ matrix: - sudo apt-get install -y python3-pip - sudo pip3 install lxml - make qt4py3 - - ( xvfb-run python3 labelImg.py ) & sleep 10 ; kill $! + - xvfb-run make testpy3 # Python 3 + QT5 - os: linux @@ -90,7 +90,7 @@ matrix: - sudo apt-get install -y python3-pip - sudo pip3 install lxml - make qt5py3 - - ( xvfb-run python3 labelImg.py ) & sleep 10 ; kill $! + - xvfb-run make testpy3 # OS X Python 3 + QT5 - os: osx @@ -112,7 +112,9 @@ matrix: - python3 -c 'import lxml' - make qt5py3 - python3 -c 'help("modules")' - - ( python3 labelImg.py ) & sleep 10 ; kill $! + #- make testpy3 # FIXME: does not work, segfault on travis-ci + # just make sure the app runs... :-/ + - ( python3 labelImg.py ) & sleep 10; kill $! script: - exit 0 diff --git a/Makefile b/Makefile index 54c88897..5b6e4d8d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,15 @@ +# ex: set ts=8 noet: all: qt4 +test: testpy2 + +testpy2: + python -m unittest discover tests + +testpy3: + python3 -m unittest discover tests + qt4: qt4py2 qt5: qt4py3 @@ -14,3 +23,4 @@ qt4py3: qt5py3: pyrcc5 -o resources.py resources.qrc +.PHONY: test diff --git a/labelImg.py b/labelImg.py index a79e1c39..5fb3f021 100755 --- a/labelImg.py +++ b/labelImg.py @@ -1187,13 +1187,21 @@ def read(filename, default=None): except: return default -def main(argv): - """Standard boilerplate Qt application code.""" +def get_main_app(argv=[]): + """ + Standard boilerplate Qt application code. + Do everything but app.exec_() -- so that we can test the application in one thread + """ app = QApplication(argv) app.setApplicationName(__appname__) app.setWindowIcon(newIcon("app")) win = MainWindow(argv[1] if len(argv) == 2 else None) win.show() + return app, win + +def main(argv): + '''construct main app and run it''' + app, _win = get_main_app(argv) return app.exec_() if __name__ == '__main__': diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 00000000..a9d64d43 --- /dev/null +++ b/tests/test.py @@ -0,0 +1,21 @@ + +from unittest import TestCase + +from labelImg import get_main_app + + +class TestMainWindow(TestCase): + + app = None + win = None + + def setUp(self): + self.app, self.win = get_main_app() + + def tearDown(self): + self.win.close() + self.app.quit() + + def test_noop(self): + pass +