Skip to content

Commit 23e7132

Browse files
authored
Merge pull request #1335 from tpaviot/review/pytst
Move test suite to pytest
2 parents 3226c3b + 0cf2a45 commit 23e7132

11 files changed

+2344
-2311
lines changed

ci/conda/run_test.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cd ..\work\test
2-
pytest
2+
pytest -sv
33
mypy test_mypy_classic_occ_bottle.py
44
python core_display_tkinter_unittest.py
55
python core_display_pyqt5_unittest.py

ci/conda/run_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
if [ "$(uname)" == "Linux" ]; then
33
cd ../work/test
4-
pytest
4+
pytest -sv
55
mypy test_mypy_classic_occ_bottle.py
66
fi

test/test_core_extend_dataexchange.py

Lines changed: 88 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1919

2020
import os
21-
import unittest
2221

2322
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus
2423
from OCC.Core.TopoDS import TopoDS_Compound
@@ -61,107 +60,113 @@ def get_test_fullname(filename):
6160
A_TOPODS_SHAPE = BRepPrimAPI_MakeTorus(200, 50).Shape()
6261

6362

64-
class TestExtendDataExchange(unittest.TestCase):
65-
def check_is_file(self, filename):
66-
self.assertTrue(os.path.isfile(filename))
63+
def check_is_file(filename):
64+
assert os.path.isfile(filename)
6765

68-
def test_read_step_file(self):
69-
read_step_file(STEP_AP203_SAMPLE_FILE)
70-
read_step_file(STEP_AP214_SAMPLE_FILE)
7166

72-
def test_read_step_file_multiple_shape_as_root(self):
73-
t = read_step_file(STEP_MULTIPLE_ROOT, as_compound=True)
74-
self.assertTrue(isinstance(t, TopoDS_Compound))
67+
def test_read_step_file():
68+
read_step_file(STEP_AP203_SAMPLE_FILE)
69+
read_step_file(STEP_AP214_SAMPLE_FILE)
7570

76-
l = read_step_file(STEP_MULTIPLE_ROOT, as_compound=False)
77-
self.assertEqual(len(l), 3)
7871

79-
def test_read_step_file_names_colors(self):
80-
read_step_file_with_names_colors(STEP_AP203_SAMPLE_FILE)
81-
read_step_file_with_names_colors(STEP_AP214_SAMPLE_FILE)
72+
def test_read_step_file_multiple_shape_as_root():
73+
t = read_step_file(STEP_MULTIPLE_ROOT, as_compound=True)
74+
assert isinstance(t, TopoDS_Compound)
8275

83-
def test_read_iges_file(self):
84-
read_iges_file(IGES_SAMPLE_FILE)
76+
l = read_step_file(STEP_MULTIPLE_ROOT, as_compound=False)
77+
assert len(l) == 3
8578

86-
def test_read_iges_45_shapes(self):
87-
all_shapes = read_iges_file(
88-
IGES_45_FACES, return_as_shapes=True, verbosity=True
89-
)
90-
self.assertEqual(len(all_shapes), 45)
9179

92-
def test_read_iges_45_shapes_as_one_compound(self):
93-
shapes = read_iges_file(IGES_45_FACES, return_as_shapes=False, verbosity=True)
94-
self.assertEqual(len(shapes), 1)
95-
self.assertIsInstance(shapes[0], TopoDS_Compound)
96-
topo_explorer = TopologyExplorer(shapes[0])
97-
self.assertEqual(topo_explorer.number_of_faces(), 45)
80+
def test_read_step_file_names_colors():
81+
read_step_file_with_names_colors(STEP_AP203_SAMPLE_FILE)
82+
read_step_file_with_names_colors(STEP_AP214_SAMPLE_FILE)
9883

99-
def test_read_stl_file(self):
100-
read_stl_file(STL_ASCII_SAMPLE_FILE)
101-
read_stl_file(STL_BINARY_SAMPLE_FILE)
10284

103-
def test_export_shape_to_svg(self):
104-
svg_filename = get_test_fullname("sample.svg")
105-
export_shape_to_svg(A_TOPODS_SHAPE, svg_filename)
106-
self.check_is_file(svg_filename)
85+
def test_read_iges_file():
86+
read_iges_file(IGES_SAMPLE_FILE)
10787

108-
def test_read_gltf_ascii_file(self):
109-
shp = read_gltf_file(GLTF_ASCII_SAMPLE_FILE)
11088

111-
def test_read_gltf_binary_file(self):
112-
shp = read_gltf_file(GLTF_BINARY_SAMPLE_FILE)
89+
def test_read_iges_45_shapes():
90+
all_shapes = read_iges_file(IGES_45_FACES, return_as_shapes=True, verbosity=True)
91+
assert len(all_shapes) == 45
11392

114-
def test_write_step_ap203(self):
115-
ap203_filename = get_test_fullname("sample_ap_203.stp")
116-
write_step_file(A_TOPODS_SHAPE, ap203_filename, application_protocol="AP203")
117-
self.check_is_file(ap203_filename)
11893

119-
def test_write_step_ap214(self):
120-
as214_filename = get_test_fullname("sample_214.stp")
121-
write_step_file(A_TOPODS_SHAPE, as214_filename, application_protocol="AP214IS")
122-
self.check_is_file(as214_filename)
94+
def test_read_iges_45_shapes_as_one_compound():
95+
shapes = read_iges_file(IGES_45_FACES, return_as_shapes=False, verbosity=True)
96+
assert len(shapes) == 1
97+
assert isinstance(shapes[0], TopoDS_Compound)
98+
topo_explorer = TopologyExplorer(shapes[0])
99+
assert topo_explorer.number_of_faces() == 45
123100

124-
def test_write_step_ap242(self):
125-
ap242_filename = get_test_fullname("sample_242.stp")
126-
write_step_file(A_TOPODS_SHAPE, ap242_filename, application_protocol="AP242DIS")
127-
self.check_is_file(ap242_filename)
128101

129-
def test_write_iges(self):
130-
iges_filename = get_test_fullname("sample.igs")
131-
write_iges_file(A_TOPODS_SHAPE, iges_filename)
132-
self.check_is_file(iges_filename)
102+
def test_read_stl_file():
103+
read_stl_file(STL_ASCII_SAMPLE_FILE)
104+
read_stl_file(STL_BINARY_SAMPLE_FILE)
133105

134-
def test_stl_ascii(self):
135-
stl_ascii_filename = get_test_fullname("sample_ascii.stl")
136-
write_stl_file(A_TOPODS_SHAPE, stl_ascii_filename, mode="ascii")
137-
self.check_is_file(stl_ascii_filename)
138106

139-
def test_stl_binary(self):
140-
stl_binary_filename = get_test_fullname("sample_binary.stl")
141-
write_stl_file(A_TOPODS_SHAPE, stl_binary_filename, mode="binary")
142-
self.check_is_file(stl_binary_filename)
107+
def test_export_shape_to_svg():
108+
svg_filename = get_test_fullname("sample.svg")
109+
export_shape_to_svg(A_TOPODS_SHAPE, svg_filename)
110+
check_is_file(svg_filename)
143111

144-
def test_write_ply(self):
145-
ply_filename = get_test_fullname("sample.ply")
146-
write_ply_file(A_TOPODS_SHAPE, ply_filename)
147-
self.check_is_file(ply_filename)
148112

149-
def test_write_obj(self):
150-
obj_filename = get_test_fullname("sample.obj")
151-
write_obj_file(A_TOPODS_SHAPE, obj_filename)
152-
self.check_is_file(obj_filename)
113+
def test_read_gltf_ascii_file():
114+
shp = read_gltf_file(GLTF_ASCII_SAMPLE_FILE)
153115

154-
def test_write_gltf(self):
155-
gltf_filename = get_test_fullname("sample.gltf")
156-
write_gltf_file(A_TOPODS_SHAPE, gltf_filename)
157-
self.check_is_file(gltf_filename)
158116

117+
def test_read_gltf_binary_file():
118+
shp = read_gltf_file(GLTF_BINARY_SAMPLE_FILE)
159119

160-
def suite():
161-
test_suite = unittest.TestSuite()
162-
test_suite.addTest(unittest.makeSuite(TestExtendDataExchange))
163-
return test_suite
164-
165-
166-
if __name__ == "__main__":
167-
unittest.main()
120+
121+
def test_write_step_ap203():
122+
ap203_filename = get_test_fullname("sample_ap_203.stp")
123+
write_step_file(A_TOPODS_SHAPE, ap203_filename, application_protocol="AP203")
124+
check_is_file(ap203_filename)
125+
126+
127+
def test_write_step_ap214():
128+
as214_filename = get_test_fullname("sample_214.stp")
129+
write_step_file(A_TOPODS_SHAPE, as214_filename, application_protocol="AP214IS")
130+
check_is_file(as214_filename)
131+
132+
133+
def test_write_step_ap242():
134+
ap242_filename = get_test_fullname("sample_242.stp")
135+
write_step_file(A_TOPODS_SHAPE, ap242_filename, application_protocol="AP242DIS")
136+
check_is_file(ap242_filename)
137+
138+
139+
def test_write_iges():
140+
iges_filename = get_test_fullname("sample.igs")
141+
write_iges_file(A_TOPODS_SHAPE, iges_filename)
142+
check_is_file(iges_filename)
143+
144+
145+
def test_stl_ascii():
146+
stl_ascii_filename = get_test_fullname("sample_ascii.stl")
147+
write_stl_file(A_TOPODS_SHAPE, stl_ascii_filename, mode="ascii")
148+
check_is_file(stl_ascii_filename)
149+
150+
151+
def test_stl_binary():
152+
stl_binary_filename = get_test_fullname("sample_binary.stl")
153+
write_stl_file(A_TOPODS_SHAPE, stl_binary_filename, mode="binary")
154+
check_is_file(stl_binary_filename)
155+
156+
157+
def test_write_ply():
158+
ply_filename = get_test_fullname("sample.ply")
159+
write_ply_file(A_TOPODS_SHAPE, ply_filename)
160+
check_is_file(ply_filename)
161+
162+
163+
def test_write_obj():
164+
obj_filename = get_test_fullname("sample.obj")
165+
write_obj_file(A_TOPODS_SHAPE, obj_filename)
166+
check_is_file(obj_filename)
167+
168+
169+
def test_write_gltf():
170+
gltf_filename = get_test_fullname("sample.gltf")
171+
write_gltf_file(A_TOPODS_SHAPE, gltf_filename)
172+
check_is_file(gltf_filename)

test/test_core_extend_shapefactory.py

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,67 +37,62 @@
3737
)
3838
from OCC.Extend.TopologyUtils import TopologyExplorer
3939

40+
import pytest
4041

41-
class TestExtendShapeFactory(unittest.TestCase):
42-
def test_midpoint(self):
43-
p1 = gp_Pnt(0, 0, 0)
44-
p2 = gp_Pnt(4, 5, 6)
45-
p3 = midpoint(p1, p2)
46-
self.assertEqual([p3.X(), p3.Y(), p3.Z()], [2, 2.5, 3.0])
4742

48-
def test_measure_shape_volume(self):
49-
# first the colume of a box a,b,c should be a*b*c
50-
a = 10.0
51-
b = 23.0
52-
c = 98.1
53-
box = BRepPrimAPI_MakeBox(a, b, c).Shape()
54-
box_volume = measure_shape_volume(box)
55-
self.assertAlmostEqual(box_volume, a * b * c, places=6)
56-
# for a sphere of radius r, it should be 4/3.pi.r^3
57-
r = 9.8775 # a random radius
58-
sph = BRepPrimAPI_MakeSphere(r).Shape()
59-
sph_volume = measure_shape_volume(sph)
60-
self.assertAlmostEqual(sph_volume, 4.0 / 3.0 * math.pi * r**3, places=6)
43+
def test_midpoint():
44+
p1 = gp_Pnt(0, 0, 0)
45+
p2 = gp_Pnt(4, 5, 6)
46+
p3 = midpoint(p1, p2)
47+
assert [p3.X(), p3.Y(), p3.Z()] == [2, 2.5, 3.0]
6148

62-
def test_scale_shape(self):
63-
box = BRepPrimAPI_MakeBox(10.0, 10.0, 10.0).Shape()
64-
box2 = scale_shape(box, 2.0, 1.0, 1.0)
65-
# volume should be double
66-
box2_volume = measure_shape_volume(box2)
67-
self.assertAlmostEqual(box2_volume, 2000.0, places=6)
6849

69-
def test_measure_shape_center_of_gravity(self):
70-
# we compute the cog of a sphere centered at a point P
71-
# then the cog must be P
72-
x, y, z = 10.0, 3.0, -2.44 # random values for point P
73-
radius = 20.0
74-
vector = gp_Vec(x, y, z)
75-
sph = translate_shp(BRepPrimAPI_MakeSphere(radius).Shape(), vector)
76-
cog, mass, mass_property = measure_shape_mass_center_of_gravity(sph)
77-
self.assertAlmostEqual(cog.X(), x, places=6)
78-
self.assertAlmostEqual(cog.Y(), y, places=6)
79-
self.assertAlmostEqual(cog.Z(), z, places=6)
80-
self.assertAlmostEqual(mass, 4 / 3 * math.pi * radius**3, places=6)
81-
self.assertEqual(mass_property, "Volume")
50+
def test_measure_shape_volume():
51+
# first the colume of a box a,b,c should be a*b*c
52+
a = 10.0
53+
b = 23.0
54+
c = 98.1
55+
box = BRepPrimAPI_MakeBox(a, b, c).Shape()
56+
box_volume = measure_shape_volume(box)
57+
assert box_volume == pytest.approx(box_volume, a * b * c)
58+
# for a sphere of radius r, it should be 4/3.pi.r^3
59+
r = 9.8775 # a random radius
60+
sph = BRepPrimAPI_MakeSphere(r).Shape()
61+
sph_volume = measure_shape_volume(sph)
62+
assert sph_volume == pytest.approx(4.0 / 3.0 * math.pi * r**3)
8263

83-
def test_edge_to_bezier(self):
84-
b = BRepPrimAPI_MakeTorus(30, 10).Shape()
85-
t = TopologyExplorer(b)
86-
for ed in t.edges():
87-
is_bezier, bezier_curve, degree = edge_to_bezier(ed)
88-
self.assertTrue(isinstance(is_bezier, bool))
89-
if not is_bezier:
90-
self.assertTrue(degree is None)
91-
self.assertTrue(bezier_curve is None)
92-
else:
93-
self.assertTrue(isinstance(degree, int))
9464

65+
def test_scale_shape():
66+
box = BRepPrimAPI_MakeBox(10.0, 10.0, 10.0).Shape()
67+
box2 = scale_shape(box, 2.0, 1.0, 1.0)
68+
# volume should be double
69+
box2_volume = measure_shape_volume(box2)
70+
assert pytest.approx(box2_volume) == 2000.0
9571

96-
def suite():
97-
test_suite = unittest.TestSuite()
98-
test_suite.addTest(unittest.makeSuite(TestExtendShapeFactory))
99-
return test_suite
10072

73+
def test_measure_shape_center_of_gravity():
74+
# we compute the cog of a sphere centered at a point P
75+
# then the cog must be P
76+
x, y, z = 10.0, 3.0, -2.44 # random values for point P
77+
radius = 20.0
78+
vector = gp_Vec(x, y, z)
79+
sph = translate_shp(BRepPrimAPI_MakeSphere(radius).Shape(), vector)
80+
cog, mass, mass_property = measure_shape_mass_center_of_gravity(sph)
81+
assert cog.X() == pytest.approx(x)
82+
assert cog.Y() == pytest.approx(y)
83+
assert cog.Z() == pytest.approx(z)
84+
assert mass == pytest.approx(4 / 3 * math.pi * radius**3)
85+
assert mass_property == "Volume"
10186

102-
if __name__ == "__main__":
103-
unittest.main()
87+
88+
def test_edge_to_bezier():
89+
b = BRepPrimAPI_MakeTorus(30, 10).Shape()
90+
t = TopologyExplorer(b)
91+
for ed in t.edges():
92+
is_bezier, bezier_curve, degree = edge_to_bezier(ed)
93+
assert isinstance(is_bezier, bool)
94+
if not is_bezier:
95+
assert degree is None
96+
assert bezier_curve is None
97+
else:
98+
assert isinstance(degree, int)

0 commit comments

Comments
 (0)