Skip to content

Commit 77d2fc3

Browse files
committed
Expand test cases to output a diff on assertEqual failing instead of the strings which are hard to understand.
1 parent c84f916 commit 77d2fc3

File tree

6 files changed

+32
-5
lines changed

6 files changed

+32
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.installed.cfg
77
bin
88
develop-eggs
9+
*.egg-info
910
dist
1011
downloads
1112
eggs

solid/test/ExpandedTestCase.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
A version of unittest that gives output in an easier to use format
3+
"""
4+
import unittest
5+
import difflib
6+
7+
8+
class DiffOutput(unittest.TestCase):
9+
def assertEqual(self, first, second, msg=None):
10+
"""
11+
Override assertEqual and print a context diff if msg=None
12+
"""
13+
if not msg:
14+
msg = 'Strings are not equal:\n' + ''.join(
15+
difflib.unified_diff(first, second, fromfile='actual', tofile='expected')
16+
)
17+
return super(DiffOutput, self).assertEqual(first, second, msg=msg)

solid/test/test_screw_thread.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#! /usr/bin/python
22
# -*- coding: UTF-8 -*-
33
from __future__ import division
4-
import os, sys, re
4+
import os
5+
import sys
6+
import re
57

68
# Assumes SolidPython is in site-packages or elsewhwere in sys.path
79
import unittest
10+
import ExpandedTestCase
811
from solid import *
912
from solid.screw_thread import thread, default_thread_section
1013

1114
SEGMENTS = 8
12-
class TestScrewThread( unittest.TestCase):
15+
class TestScrewThread(ExpandedTestCase.DiffOutput):
1316
def test_thread( self):
1417
tooth_height = 10
1518
tooth_depth = 5

solid/test/test_solidpython.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import unittest
66
import tempfile
7+
import ExpandedTestCase
78
from solid import *
89

910
scad_test_case_templates = [
@@ -35,7 +36,7 @@
3536
{'name': 'intersection_for', 'kwargs': {}, 'expected': '\n\nintersection_for(n = [0, 1, 2]);', 'args': {'n': [0, 1, 2]}, },
3637
]
3738

38-
class TestSolidPython( unittest.TestCase):
39+
class TestSolidPython(ExpandedTestCase.DiffOutput):
3940
# test cases will be dynamically added to this instance
4041
def test_infix_union( self):
4142
a = cube(2)

solid/test/test_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from solid import *
88
from solid.utils import *
99
from euclid import *
10+
import difflib
11+
import ExpandedTestCase
12+
1013

1114
tri = [Point3( 0,0,0), Point3( 10,0,0), Point3(0,10,0)]
1215

@@ -49,7 +52,7 @@
4952
]
5053

5154

52-
class TestSPUtils( unittest.TestCase):
55+
class TestSPUtils(ExpandedTestCase.DiffOutput):
5356
# Test cases will be dynamically added to this instance
5457
# using the test case arrays above
5558

@@ -89,6 +92,8 @@ def test_fillet_2d_remove( self):
8992
newp = fillet_2d( tri, orig_poly=poly, fillet_rad=2, remove_material=True)
9093
expected = '\n\ndifference() {\n\tpolygon(paths = [[0, 1, 2]], points = [[0, 0, 0], [10, 0, 0], [0, 10, 0]]);\n\ttranslate(v = [5.1715728753, 2.0000000000, 0.0000000000]) {\n\t\tdifference() {\n\t\t\tintersection() {\n\t\t\t\trotate(a = 268.0000000000) {\n\t\t\t\t\ttranslate(v = [-998, 0]) {\n\t\t\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\trotate(a = 407.0000000000) {\n\t\t\t\t\ttranslate(v = [-998, -1000]) {\n\t\t\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcircle(r = 2);\n\t\t}\n\t}\n}'
9194
actual = scad_render( newp)
95+
if expected != actual:
96+
print(''.join(difflib.unified_diff(expected, actual)))
9297
self.assertEqual( expected, actual)
9398

9499

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# content of: tox.ini , put in same dir as setup.py
22
[tox]
3-
envlist = py26,py27
3+
envlist = py27
44

55
[testenv]
66
commands=

0 commit comments

Comments
 (0)