فهرست منبع

Add test for labelme/app.py

Kentaro Wada 7 سال پیش
والد
کامیت
6e1402bccf
4فایلهای تغییر یافته به همراه92 افزوده شده و 8 حذف شده
  1. 1 0
      labelme/__init__.py
  2. 25 0
      labelme/testing.py
  3. 58 0
      tests/test_app.py
  4. 8 8
      tests/test_labelDialog.py

+ 1 - 0
labelme/__init__.py

@@ -1,3 +1,4 @@
 # flake8: noqa
 
+from labelme import testing
 from labelme import utils

+ 25 - 0
labelme/testing.py

@@ -0,0 +1,25 @@
+import json
+import os.path as osp
+
+import labelme.utils
+
+
+def assert_labelfile_sanity(filename):
+    assert osp.exists(filename)
+
+    data = json.load(open(filename))
+
+    assert 'imagePath' in data
+    imageData = data.get('imageData', None)
+    if imageData is None:
+        assert osp.exists(data['imagePath'])
+    img = labelme.utils.img_b64_to_arr(imageData)
+
+    H, W = img.shape[:2]
+    assert 'shapes' in data
+    for shape in data['shapes']:
+        assert 'label' in shape
+        assert 'points' in shape
+        for x, y in shape['points']:
+            assert 0 <= x <= W
+            assert 0 <= y <= H

+ 58 - 0
tests/test_app.py

@@ -0,0 +1,58 @@
+import os.path as osp
+import shutil
+import tempfile
+
+import labelme.app
+import labelme.testing
+
+
+here = osp.dirname(osp.abspath(__file__))
+data_dir = osp.join(here, 'data')
+
+
+def test_MainWindow_open(qtbot):
+    win = labelme.app.MainWindow()
+    qtbot.addWidget(win)
+    win.show()
+    win.close()
+
+
+def test_MainWindow_open_json(qtbot):
+    filename = osp.join(data_dir, 'apc2016_obj3.json')
+    labelme.testing.assert_labelfile_sanity(filename)
+    win = labelme.app.MainWindow(filename=filename)
+    qtbot.addWidget(win)
+    win.show()
+    win.close()
+
+
+def test_MainWindow_annotate_jpg(qtbot):
+    tmp_dir = tempfile.mkdtemp()
+    filename = osp.join(tmp_dir, 'apc2016_obj3.jpg')
+    shutil.copy(osp.join(data_dir, 'apc2016_obj3.jpg'),
+                filename)
+    output = osp.join(tmp_dir, 'apc2016_obj3.json')
+
+    win = labelme.app.MainWindow(filename=filename, output=output)
+    qtbot.addWidget(win)
+    win.show()
+
+    def check_imageData():
+        assert hasattr(win, 'imageData')
+        assert win.imageData is not None
+
+    qtbot.waitUntil(check_imageData)  # wait for loadFile
+
+    label = 'shelf'
+    points = [
+        (26, 70),
+        (176, 730),
+        (986, 742),
+        (1184, 102),
+    ]
+    shape = label, points, None, None
+    shapes = [shape]
+    win.loadLabels(shapes)
+    win.saveFile()
+
+    labelme.testing.assert_labelfile_sanity(output)

+ 8 - 8
tests/test_labelDialog.py

@@ -57,9 +57,9 @@ def test_LabelDialog_popUp(qtbot):
     # popUp(text='cat')
 
     def interact():
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_P)  # enter 'p' for 'person'
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_P)  # enter 'p' for 'person'  # NOQA
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)  # NOQA
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)  # NOQA
 
     QtCore.QTimer.singleShot(500, interact)
     text = widget.popUp('cat')
@@ -68,8 +68,8 @@ def test_LabelDialog_popUp(qtbot):
     # popUp()
 
     def interact():
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)  # NOQA
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)  # NOQA
 
     QtCore.QTimer.singleShot(500, interact)
     text = widget.popUp()
@@ -78,9 +78,9 @@ def test_LabelDialog_popUp(qtbot):
     # popUp() + key_Up
 
     def interact():
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Up)  # 'person' -> 'dog'
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)
-        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Up)  # 'person' -> 'dog'  # NOQA
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)  # NOQA
+        qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter)  # NOQA
 
     QtCore.QTimer.singleShot(500, interact)
     text = widget.popUp()