diff --git a/src/Mod/Draft/CMakeLists.txt b/src/Mod/Draft/CMakeLists.txt index 3a70b64b33..080a68063d 100644 --- a/src/Mod/Draft/CMakeLists.txt +++ b/src/Mod/Draft/CMakeLists.txt @@ -48,22 +48,23 @@ SET (Draft_geoutils SET(Draft_tests drafttests/__init__.py + drafttests/README.md drafttests/auxiliary.py + drafttests/draft_test_objects.py + drafttests/test_airfoildat.py + drafttests/test_array.py + drafttests/test_base.py + drafttests/test_creation.py + drafttests/test_draftgeomutils.py + drafttests/test_dwg.py + drafttests/test_dxf.py drafttests/test_import.py drafttests/test_import_gui.py drafttests/test_import_tools.py - drafttests/test_pivy.py - drafttests/test_array.py - drafttests/test_creation.py drafttests/test_modification.py - drafttests/test_svg.py - drafttests/test_dxf.py - drafttests/test_dwg.py drafttests/test_oca.py - drafttests/test_airfoildat.py - drafttests/test_draftgeomutils.py - drafttests/draft_test_objects.py - drafttests/README.md + drafttests/test_pivy.py + drafttests/test_svg.py ) SET(Draft_utilities diff --git a/src/Mod/Draft/drafttests/test_airfoildat.py b/src/Mod/Draft/drafttests/test_airfoildat.py index 34905b724d..7311a91750 100644 --- a/src/Mod/Draft/drafttests/test_airfoildat.py +++ b/src/Mod/Draft/drafttests/test_airfoildat.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -29,35 +30,18 @@ ## \addtogroup drafttests # @{ import os -import unittest import FreeCAD as App import Draft import drafttests.auxiliary as aux from draftutils.messages import _msg +from drafttests import test_base -class DraftAirfoilDAT(unittest.TestCase): +class DraftAirfoilDAT(test_base.DraftTestCaseDoc): """Test reading and writing of AirfoilDAT with Draft.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - self.doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != self.doc_name: - App.newDocument(self.doc_name) - else: - App.newDocument(self.doc_name) - App.setActiveDocument(self.doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc_name)) - def test_read_airfoildat(self): """Read an airfoil DAT file and import its elements as objects.""" operation = "importAirfoilDAT.import" @@ -87,11 +71,4 @@ class DraftAirfoilDAT(unittest.TestCase): obj = Draft.export_airfoildat(out_file) self.assertTrue(obj, "'{}' failed".format(operation)) - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc_name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_array.py b/src/Mod/Draft/drafttests/test_array.py index 27b548842e..b0a4e71752 100644 --- a/src/Mod/Draft/drafttests/test_array.py +++ b/src/Mod/Draft/drafttests/test_array.py @@ -1,5 +1,6 @@ # *************************************************************************** # * Copyright (c) 2023 Werner Mayer * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of FreeCAD. * # * * @@ -25,7 +26,6 @@ ## \addtogroup drafttests # @{ -import unittest import math import FreeCAD as App @@ -33,27 +33,12 @@ import Draft from FreeCAD import Vector from draftutils.messages import _msg +from drafttests import test_base -class DraftArray(unittest.TestCase): +class DraftArray(test_base.DraftTestCaseDoc): """Test Draft array functions.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != doc_name: - App.newDocument(doc_name) - else: - App.newDocument(doc_name) - App.setActiveDocument(doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc.Name)) - def test_link_array(self): """Create a link array.""" box = self.doc.addObject("Part::Box","Box") @@ -81,11 +66,4 @@ class DraftArray(unittest.TestCase): self.doc.recompute(None,True,True) self.assertEqual(array.Count, array.NumberX) - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc.Name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_base.py b/src/Mod/Draft/drafttests/test_base.py new file mode 100644 index 0000000000..9d07424d08 --- /dev/null +++ b/src/Mod/Draft/drafttests/test_base.py @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# *************************************************************************** +# * * +# * Copyright (c) 2025 FreeCAD Project Association * +# * * +# * This file is part of FreeCAD. * +# * * +# * FreeCAD is free software: you can redistribute it and/or modify it * +# * under the terms of the GNU Lesser General Public License as * +# * published by the Free Software Foundation, either version 2.1 of the * +# * License, or (at your option) any later version. * +# * * +# * FreeCAD is distributed in the hope that it will be useful, but * +# * WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * +# * Lesser General Public License for more details. * +# * * +# * You should have received a copy of the GNU Lesser General Public * +# * License along with FreeCAD. If not, see * +# * . * +# * * +# *************************************************************************** + +import unittest + +import FreeCAD as App +from drafttests import auxiliary as aux +from draftutils.messages import _msg +from draftutils.todo import ToDo + + +class DraftTestCaseDoc(unittest.TestCase): + """Base class for Draft tests that require a document.""" + + def setUp(self): + """Set up a new document for each test.""" + aux.draw_header() + # name = self.__class__.__name__ + name = "___".join(self.id().split(".")[2:]) # unique name for each test + if not name in App.listDocuments(): + App.newDocument(name) + App.setActiveDocument(name) + self.doc = App.ActiveDocument + _msg(" Temporary document '{}'".format(self.doc.Name)) + + def tearDown(self): + """Close the document after each test.""" + App.closeDocument(self.doc.Name) + + +class DraftTestCaseNoDoc(unittest.TestCase): + """Base class for Draft tests that do not require a document.""" + + def setUp(self): + """Draw the header.""" + aux.draw_header() diff --git a/src/Mod/Draft/drafttests/test_creation.py b/src/Mod/Draft/drafttests/test_creation.py index 2ef1aa0729..30a484c410 100644 --- a/src/Mod/Draft/drafttests/test_creation.py +++ b/src/Mod/Draft/drafttests/test_creation.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -28,7 +29,6 @@ ## \addtogroup drafttests # @{ -import unittest import math import FreeCAD as App @@ -37,28 +37,12 @@ import drafttests.auxiliary as aux from FreeCAD import Vector from draftutils.messages import _msg +from drafttests import test_base -class DraftCreation(unittest.TestCase): +class DraftCreation(test_base.DraftTestCaseDoc): """Test Draft creation functions.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != doc_name: - App.newDocument(doc_name) - else: - App.newDocument(doc_name) - App.setActiveDocument(doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc.Name)) - def test_line(self): """Create a line.""" operation = "Draft Line" @@ -282,7 +266,7 @@ class DraftCreation(unittest.TestCase): _msg(" or an App::PropertyLinkSubList") _msg(" Box") - box = App.ActiveDocument.addObject("Part::Box") + box = self.doc.addObject("Part::Box") self.doc.recompute() # The facebinder function accepts a Gui selection set, # or a 'PropertyLinkSubList' @@ -378,14 +362,14 @@ class DraftCreation(unittest.TestCase): _msg(" length={0}, width={1}".format(length, width)) rect = Draft.make_rectangle(length, width) rect.MakeFace = True - App.ActiveDocument.recompute() + self.doc.recompute() patfile = App.getResourceDir() + "Mod/TechDraw/PAT/FCPAT.pat" patname = "Horizontal5" _msg(" patfile='{0}'".format(patfile)) _msg(" patname='{0}'".format(patname)) obj = Draft.make_hatch(rect, patfile, patname, scale=1, rotation=45) - App.ActiveDocument.recompute() + self.doc.recompute() box = obj.Shape.BoundBox # A rather high tolerance is required. @@ -394,11 +378,4 @@ class DraftCreation(unittest.TestCase): and math.isclose(box.YLength, width, rel_tol=0, abs_tol=1e-6)) self.assertTrue(obj_is_ok, "'{}' failed".format(operation)) - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc.Name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_draftgeomutils.py b/src/Mod/Draft/drafttests/test_draftgeomutils.py index 120584f334..12e671b191 100644 --- a/src/Mod/Draft/drafttests/test_draftgeomutils.py +++ b/src/Mod/Draft/drafttests/test_draftgeomutils.py @@ -1,5 +1,6 @@ # *************************************************************************** # * Copyright (c) 2020 Antoine Lafr * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -22,20 +23,16 @@ # *************************************************************************** """Unit test for the DraftGeomUtils module.""" -import unittest import FreeCAD import Part import DraftGeomUtils import drafttests.auxiliary as aux from draftutils.messages import _msg +from drafttests import test_base -class TestDraftGeomUtils(unittest.TestCase): +class TestDraftGeomUtils(test_base.DraftTestCaseNoDoc): """Testing the functions in the file DraftGeomUtils.py""" - def setUp(self): - """Prepare the test. Nothing to do here, DraftGeomUtils doesn't need a document.""" - aux.draw_header() - def check_wire(self, wire): offset_values = (2000.0, 0.0, -1000, -2000, -3000, -5500) for offset_start in offset_values: @@ -215,9 +212,5 @@ class TestDraftGeomUtils(unittest.TestCase): wire.Orientation = "Reversed" self.check_wire(wire) - def tearDown(self): - """Finish the test. Nothing to do here, DraftGeomUtils doesn't need a document.""" - pass - # suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestDraftGeomUtils) # unittest.TextTestRunner().run(suite) diff --git a/src/Mod/Draft/drafttests/test_dwg.py b/src/Mod/Draft/drafttests/test_dwg.py index 7e915580b4..195d6f9135 100644 --- a/src/Mod/Draft/drafttests/test_dwg.py +++ b/src/Mod/Draft/drafttests/test_dwg.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -29,35 +30,18 @@ ## \addtogroup drafttests # @{ import os -import unittest import FreeCAD as App import Draft import drafttests.auxiliary as aux from draftutils.messages import _msg +from drafttests import test_base -class DraftDWG(unittest.TestCase): +class DraftDWG(test_base.DraftTestCaseDoc): """Test reading and writing of DWG files with Draft.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - self.doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != self.doc_name: - App.newDocument(self.doc_name) - else: - App.newDocument(self.doc_name) - App.setActiveDocument(self.doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc_name)) - def test_read_dwg(self): """Read a DWG file and import its elements as Draft objects.""" operation = "importDWG.import" @@ -87,11 +71,4 @@ class DraftDWG(unittest.TestCase): obj = Draft.export_dwg(out_file) self.assertTrue(obj, "'{}' failed".format(operation)) - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc_name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_dxf.py b/src/Mod/Draft/drafttests/test_dxf.py index 8f6a4defde..ddb73c58b7 100644 --- a/src/Mod/Draft/drafttests/test_dxf.py +++ b/src/Mod/Draft/drafttests/test_dxf.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -29,35 +30,18 @@ ## \addtogroup drafttests # @{ import os -import unittest import FreeCAD as App import Draft import drafttests.auxiliary as aux from draftutils.messages import _msg +from drafttests import test_base -class DraftDXF(unittest.TestCase): +class DraftDXF(test_base.DraftTestCaseDoc): """Test reading and writing of DXF files with Draft.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - self.doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != self.doc_name: - App.newDocument(self.doc_name) - else: - App.newDocument(self.doc_name) - App.setActiveDocument(self.doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc_name)) - def test_read_dxf(self): """Read a DXF file and import its elements as Draft objects.""" operation = "importDXF.import" @@ -87,11 +71,4 @@ class DraftDXF(unittest.TestCase): obj = Draft.export_dxf(out_file) self.assertTrue(obj, "'{}' failed".format(operation)) - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc_name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_import.py b/src/Mod/Draft/drafttests/test_import.py index 3d68149278..08f69191d9 100644 --- a/src/Mod/Draft/drafttests/test_import.py +++ b/src/Mod/Draft/drafttests/test_import.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -28,23 +29,13 @@ ## \addtogroup drafttests # @{ -import unittest - import drafttests.auxiliary as aux +from drafttests import test_base -class DraftImport(unittest.TestCase): +class DraftImport(test_base.DraftTestCaseNoDoc): """Import the Draft modules.""" - def setUp(self): - """Draw the header. - - This is executed before every test. - No document is needed to test the import of modules so no document - is created, and `tearDown` isn't defined. - """ - aux.draw_header() - def test_import_draft(self): """Import the Draft module.""" module = "Draft" diff --git a/src/Mod/Draft/drafttests/test_import_gui.py b/src/Mod/Draft/drafttests/test_import_gui.py index 3e9c310211..b145029b03 100644 --- a/src/Mod/Draft/drafttests/test_import_gui.py +++ b/src/Mod/Draft/drafttests/test_import_gui.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -28,23 +29,13 @@ ## \addtogroup drafttests # @{ -import unittest - import drafttests.auxiliary as aux +from drafttests import test_base -class DraftGuiImport(unittest.TestCase): +class DraftGuiImport(test_base.DraftTestCaseNoDoc): """Import the Draft graphical modules.""" - def setUp(self): - """Draw the header. - - This is executed before every test. - No document is needed to test the import of modules so no document - is created, and `tearDown` isn't defined. - """ - aux.draw_header() - def test_import_gui_draftgui(self): """Import Draft TaskView GUI tools.""" module = "DraftGui" diff --git a/src/Mod/Draft/drafttests/test_import_tools.py b/src/Mod/Draft/drafttests/test_import_tools.py index 9e970c7c33..2e0dfe9bd4 100644 --- a/src/Mod/Draft/drafttests/test_import_tools.py +++ b/src/Mod/Draft/drafttests/test_import_tools.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -28,23 +29,13 @@ ## \addtogroup drafttests # @{ -import unittest - import drafttests.auxiliary as aux +from drafttests import test_base -class DraftImportTools(unittest.TestCase): +class DraftImportTools(test_base.DraftTestCaseNoDoc): """Test for each individual module that defines a tool.""" - def setUp(self): - """Draw the header. - - This is executed before every test. - No document is needed to test the import of modules so no document - is created, and `tearDown` isn't defined. - """ - aux.draw_header() - def test_import_gui_draftedit(self): """Import Draft Edit.""" module = "draftguitools.gui_edit" diff --git a/src/Mod/Draft/drafttests/test_modification.py b/src/Mod/Draft/drafttests/test_modification.py index 2f4e9c217f..9bf41bdfe9 100644 --- a/src/Mod/Draft/drafttests/test_modification.py +++ b/src/Mod/Draft/drafttests/test_modification.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -28,8 +29,6 @@ ## \addtogroup drafttests # @{ -import unittest - import FreeCAD as App import Draft import drafttests.auxiliary as aux @@ -37,28 +36,12 @@ import Part from FreeCAD import Vector from draftutils.messages import _msg, _wrn +from drafttests import test_base -class DraftModification(unittest.TestCase): +class DraftModification(test_base.DraftTestCaseDoc): """Test Draft modification tools.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - self.doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != self.doc_name: - App.newDocument(self.doc_name) - else: - App.newDocument(self.doc_name) - App.setActiveDocument(self.doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc_name)) - def test_move(self): """Create a line and move it.""" operation = "Draft Move" @@ -68,13 +51,13 @@ class DraftModification(unittest.TestCase): _msg(" Line") _msg(" a={0}, b={1}".format(a, b)) obj = Draft.make_line(a, b) - App.ActiveDocument.recompute() + self.doc.recompute() c = Vector(3, 1, 0) _msg(" Translation vector") _msg(" c={}".format(c)) Draft.move(obj, c) - App.ActiveDocument.recompute() + self.doc.recompute() self.assertTrue(obj.Start.isEqual(Vector(3, 3, 0), 1e-6), "'{}' failed".format(operation)) @@ -103,14 +86,14 @@ class DraftModification(unittest.TestCase): _msg(" Line") _msg(" a={0}, b={1}".format(a, b)) obj = Draft.make_line(a, b) - App.ActiveDocument.recompute() + self.doc.recompute() c = Vector(-1, 1, 0) rot = 90 _msg(" Rotation") _msg(" angle={} degrees".format(rot)) Draft.rotate(obj, rot) - App.ActiveDocument.recompute() + self.doc.recompute() self.assertTrue(obj.Start.isEqual(c, 1e-6), "'{}' failed".format(operation)) @@ -125,7 +108,7 @@ class DraftModification(unittest.TestCase): _msg(" a={0}, b={1}".format(a, b)) _msg(" c={0}".format(c)) wire = Draft.make_wire([a, b, c]) - App.ActiveDocument.recompute() + self.doc.recompute() offset = Vector(-1, 1, 0) _msg(" Offset") @@ -149,7 +132,7 @@ class DraftModification(unittest.TestCase): Part.makeLine(c, d), Part.makeLine(a, d)] wire = Part.Wire(edges) - obj = App.ActiveDocument.addObject("Part::Feature") + obj = self.doc.addObject("Part::Feature") obj.Shape = wire offset = Vector(0, -1, 0) @@ -168,13 +151,13 @@ class DraftModification(unittest.TestCase): _msg(" length={0}, width={1}".format(length, width)) rect = Draft.make_rectangle(length, width) rect.MakeFace = True - App.ActiveDocument.recompute() + self.doc.recompute() offset = Vector(0, -1, 0) _msg(" Offset") _msg(" vector={}".format(offset)) obj = Draft.offset(rect, offset, copy=True) - App.ActiveDocument.recompute() + self.doc.recompute() obj_is_ok = (obj.Shape.CenterOfGravity == Vector(5, 2, 0) and obj.Length == 12 and obj.Height == 6) @@ -196,7 +179,7 @@ class DraftModification(unittest.TestCase): _msg(" Line 2") _msg(" c={0}, d={1}".format(c, d)) line2 = Draft.make_line(c, d) - App.ActiveDocument.recompute() + self.doc.recompute() Draft.trim_objects = aux.fake_function obj = Draft.trim_objects(line, line2) @@ -217,7 +200,7 @@ class DraftModification(unittest.TestCase): _msg(" Line 2") _msg(" c={0}, d={1}".format(c, d)) line2 = Draft.make_line(c, d) - App.ActiveDocument.recompute() + self.doc.recompute() Draft.extrude = aux.fake_function obj = Draft.extrude(line, line2) @@ -260,7 +243,7 @@ class DraftModification(unittest.TestCase): obj = Draft.split(wire, b, index) # TODO: split needs to be modified so that it returns True or False. # Then checking for Wire001 is not needed - if App.ActiveDocument.Wire001: + if self.doc.Wire001: obj = True self.assertTrue(obj, "'{}' failed".format(operation)) @@ -277,29 +260,29 @@ class DraftModification(unittest.TestCase): _msg(" b={0}, c={1}".format(b, c)) shape_line_1 = Part.makeLine(a, b) shape_line_2 = Part.makeLine(b, c) - line_1 = App.ActiveDocument.addObject("Part::Feature") - line_2 = App.ActiveDocument.addObject("Part::Feature") + line_1 = self.doc.addObject("Part::Feature") + line_2 = self.doc.addObject("Part::Feature") line_1.Shape = shape_line_1 line_2.Shape = shape_line_2 - App.ActiveDocument.recompute() + self.doc.recompute() # upgrade to wire obj = Draft.upgrade([line_1, line_2], delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() s = obj[0][0] _msg(" 1: Result '{0}' ({1})".format(s.Shape.ShapeType, s.TypeId)) self.assertTrue(bool(obj[0]), "'{}' failed".format(operation)) # upgrade to closed wire obj2 = Draft.upgrade(obj[0], delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() s2 = obj2[0][0] _msg(" 2: Result '{0}' ({1})".format(s2.Shape.ShapeType, s2.TypeId)) self.assertTrue(bool(obj2[0]), "'{}' failed".format(operation)) # upgrade to face obj3 = Draft.upgrade(obj2[0], delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() s3 = obj3[0][0] _msg(" 3: Result '{0}' ({1})".format(s3.Shape.ShapeType, s3.TypeId)) self.assertTrue(bool(obj3[0]), "'{}' failed".format(operation)) @@ -307,15 +290,15 @@ class DraftModification(unittest.TestCase): # upgrade to Draft_Wire Draft.upgrade(obj3[0], delete=True) # when draftifying, upgrade doesn't return a new object - wire = App.ActiveDocument.ActiveObject + wire = self.doc.ActiveObject wire.MakeFace = True # make test independent of fillmode parameter - App.ActiveDocument.recompute() + self.doc.recompute() _msg(" 4: Result '{0}' ({1})".format(wire.Proxy.Type, wire.TypeId)) self.assertTrue(bool(wire), "'{}' failed".format(operation)) # Draft_Wire with face cannot be upgraded obj4 = Draft.upgrade(wire, delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() _msg(" The last object cannot be upgraded further") self.assertFalse(bool(obj4[0]), "'{}' failed".format(operation)) @@ -331,25 +314,25 @@ class DraftModification(unittest.TestCase): _msg(" c={0}, a={1}".format(c, a)) wire = Draft.make_wire([a, b, c, a]) wire.MakeFace = True # make test independent of fillmode parameter - App.ActiveDocument.recompute() + self.doc.recompute() # downgrade to face obj = Draft.downgrade(wire, delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() s = obj[0][0] _msg(" 1: Result '{0}' ({1})".format(s.Shape.ShapeType, s.TypeId)) self.assertTrue(bool(obj[0]), "'{}' failed".format(operation)) # downgrade to wire obj2 = Draft.downgrade(obj[0], delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() s2 = obj2[0][0] _msg(" 2: Result '{0}' ({1})".format(s2.Shape.ShapeType, s2.TypeId)) self.assertTrue(bool(obj2[0]), "'{}' failed".format(operation)) # downgrade to edges obj3 = Draft.downgrade(obj2[0], delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() s3 = obj3[0][0] _msg(" 3: Result 3 x '{0}' ({1})".format(s3.Shape.ShapeType, s3.TypeId)) @@ -357,7 +340,7 @@ class DraftModification(unittest.TestCase): # edges cannot be downgraded obj4 = Draft.downgrade(obj3[0], delete=True) - App.ActiveDocument.recompute() + self.doc.recompute() s4 = obj4[0] _msg(" 4: Result '{}'".format(s4)) _msg(" The last objects cannot be downgraded further") @@ -376,7 +359,7 @@ class DraftModification(unittest.TestCase): wire = Draft.make_wire([a, b, c]) obj = Draft.make_bspline(wire.Points) - App.ActiveDocument.recompute() + self.doc.recompute() _msg(" 1: Result '{0}' ({1})".format(obj.Proxy.Type, obj.TypeId)) self.assertTrue(obj, "'{}' failed".format(operation)) @@ -388,7 +371,7 @@ class DraftModification(unittest.TestCase): """Create a prism and then a 2D projection of it.""" operation = "Draft Shape2DView" _msg(" Test '{}'".format(operation)) - prism = App.ActiveDocument.addObject("Part::Prism") + prism = self.doc.addObject("Part::Prism") prism.Polygon = 5 # Rotate the prism 45 degrees around the Y axis prism.Placement.Rotation.Axis = Vector(0, 1, 0) @@ -414,16 +397,16 @@ class DraftModification(unittest.TestCase): _msg(" a={0}, b={1}".format(a, b)) _msg(" c={}".format(c)) wire = Draft.make_wire([a, b, c]) - App.ActiveDocument.recompute() + self.doc.recompute() obj = Draft.make_sketch(wire, autoconstraints=True) - App.ActiveDocument.recompute() + self.doc.recompute() _msg(" 1: Result '{0}' ({1})".format(obj.Shape.ShapeType, obj.TypeId)) self.assertTrue(obj, "'{}' failed".format(operation)) obj2 = Draft.draftify(obj, delete=False) - App.ActiveDocument.recompute() + self.doc.recompute() _msg(" 2: Result '{0}' ({1})".format(obj2.Proxy.Type, obj2.TypeId)) self.assertTrue(obj2, "'{}' failed".format(operation)) @@ -437,7 +420,7 @@ class DraftModification(unittest.TestCase): _msg(" Rectangle") _msg(" length={0}, width={1}".format(length, width)) rect = Draft.make_rectangle(length, width) - App.ActiveDocument.recompute() + self.doc.recompute() dir_x = Vector(5, 0, 0) dir_y = Vector(0, 4, 0) @@ -466,7 +449,7 @@ class DraftModification(unittest.TestCase): _msg(" Rectangle") _msg(" length={0}, width={1}".format(length, width)) rect = Draft.make_rectangle(length, width) - App.ActiveDocument.recompute() + self.doc.recompute() center = Vector(-4, 0, 0) angle = 180 @@ -487,7 +470,7 @@ class DraftModification(unittest.TestCase): _msg(" Rectangle") _msg(" length={0}, width={1}".format(length, width)) rect = Draft.make_rectangle(length, width) - App.ActiveDocument.recompute() + self.doc.recompute() rad_distance = 10 tan_distance = 8 @@ -574,8 +557,8 @@ class DraftModification(unittest.TestCase): """ operation = "Draft Clone" _msg(" Test '{}'".format(operation)) - box = App.ActiveDocument.addObject("Part::Box") - App.ActiveDocument.recompute() + box = self.doc.addObject("Part::Box") + self.doc.recompute() _msg(" object: '{0}' ({1})".format(box.Shape.ShapeType, box.TypeId)) obj = Draft.make_clone(box) @@ -592,18 +575,18 @@ class DraftModification(unittest.TestCase): operation = "Check attached Draft Clone behavior" _msg(" Test '{}'".format(operation)) - box1 = App.ActiveDocument.addObject("Part::Box") + box1 = self.doc.addObject("Part::Box") box1.Length = 10 - box2 = App.ActiveDocument.addObject("Part::Box") - App.ActiveDocument.recompute() + box2 = self.doc.addObject("Part::Box") + self.doc.recompute() obj = Draft.make_clone(box1) obj.MapMode = "ObjectXY" obj.AttachmentSupport = [(box2, ("",))] - App.ActiveDocument.recompute() + self.doc.recompute() box1.Length = 1 - App.ActiveDocument.recompute() + self.doc.recompute() self.assertTrue(obj.Shape.BoundBox.XLength == 1, "'{}' failed".format(operation)) @@ -611,7 +594,7 @@ class DraftModification(unittest.TestCase): """Create a solid, and then a DraftView on a TechDraw page.""" operation = "TechDraw DraftView (relies on Draft code)" _msg(" Test '{}'".format(operation)) - prism = App.ActiveDocument.addObject("Part::Prism") + prism = self.doc.addObject("Part::Prism") prism.Polygon = 5 # Rotate the prism 45 degrees around the Y axis prism.Placement.Rotation.Axis = Vector(0, 1, 0) @@ -620,14 +603,14 @@ class DraftModification(unittest.TestCase): _msg(" n_sides={}".format(prism.Polygon)) _msg(" placement={}".format(prism.Placement)) - page = App.ActiveDocument.addObject("TechDraw::DrawPage") + page = self.doc.addObject("TechDraw::DrawPage") _msg(" page={}".format(page.TypeId)) - template = App.ActiveDocument.addObject("TechDraw::DrawSVGTemplate") + template = self.doc.addObject("TechDraw::DrawSVGTemplate") template.Template = App.getResourceDir() \ + "Mod/TechDraw/Templates/A3_Landscape_blank.svg" page.Template = template _msg(" template={}".format(template.TypeId)) - view = App.ActiveDocument.addObject("TechDraw::DrawViewDraft") + view = self.doc.addObject("TechDraw::DrawViewDraft") view.Source = prism view.Direction = App.Vector(0, 0, 1) page.addView(view) @@ -644,7 +627,7 @@ class DraftModification(unittest.TestCase): _msg(" Rectangle") _msg(" length={0}, width={1}".format(length, width)) rect = Draft.make_rectangle(length, width) - # App.ActiveDocument.recompute() + # self.doc.recompute() p1 = Vector(6, -2, 0) p2 = Vector(6, 2, 0) @@ -691,12 +674,12 @@ class DraftModification(unittest.TestCase): Part.Arc(ends[1], mids[1], ends[2]), Part.Arc(ends[2], mids[2], ends[3]), Part.Arc(ends[3], mids[3], ends[0])]) - obj = App.ActiveDocument.addObject("Part::Feature") + obj = self.doc.addObject("Part::Feature") obj.Shape = shp obj.Placement.Base = base - App.ActiveDocument.recompute() + self.doc.recompute() Draft.scale([obj], sca, cen, False) - App.ActiveDocument.recompute() + self.doc.recompute() # check endpoints of arcs: newEnds = [Vector( 5.0, 5.5, 0.0), @@ -735,12 +718,12 @@ class DraftModification(unittest.TestCase): Part.LineSegment(pts[1], pts[2]), Part.LineSegment(pts[2], pts[3]), Part.LineSegment(pts[3], pts[0])]) - obj = App.ActiveDocument.addObject("Part::Feature") + obj = self.doc.addObject("Part::Feature") obj.Shape = shp obj.Placement.Base = base - App.ActiveDocument.recompute() + self.doc.recompute() Draft.scale([obj], sca, cen, False) - App.ActiveDocument.recompute() + self.doc.recompute() newPts = [Vector( 5.0, 5.5, 0.0), Vector(13.0, 5.5, 0.0), @@ -764,9 +747,9 @@ class DraftModification(unittest.TestCase): obj = Draft.make_rectangle(len, hgt) obj.Placement.Base = base - App.ActiveDocument.recompute() + self.doc.recompute() Draft.scale([obj], sca, cen, False) - App.ActiveDocument.recompute() + self.doc.recompute() newBase = Vector(5.0, 5.5, 0.0) newLen = 8.0 @@ -796,9 +779,9 @@ class DraftModification(unittest.TestCase): obj = Draft.make_bspline(pts, False) obj.Placement.Base = base - App.ActiveDocument.recompute() + self.doc.recompute() Draft.scale([obj], sca, cen, False) - App.ActiveDocument.recompute() + self.doc.recompute() newPts = [Vector( 5.0, 5.5, 0.0), Vector( 9.0, 14.5, 0.0), @@ -822,9 +805,9 @@ class DraftModification(unittest.TestCase): obj = Draft.make_wire(pts, True) obj.Placement.Base = base - App.ActiveDocument.recompute() + self.doc.recompute() Draft.scale([obj], sca, cen, False) - App.ActiveDocument.recompute() + self.doc.recompute() newPts = [Vector( 5.0, 5.5, 0.0), Vector(13.0, 5.5, 0.0), @@ -835,11 +818,4 @@ class DraftModification(unittest.TestCase): self.assertTrue(vrts[i].Point.isEqual(newPts[i], 1e-6), "'{}' failed".format(operation)) - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc_name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_oca.py b/src/Mod/Draft/drafttests/test_oca.py index 3119bdc5ab..695adb9ec1 100644 --- a/src/Mod/Draft/drafttests/test_oca.py +++ b/src/Mod/Draft/drafttests/test_oca.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -29,35 +30,18 @@ ## \addtogroup drafttests # @{ import os -import unittest import FreeCAD as App import Draft import drafttests.auxiliary as aux from draftutils.messages import _msg +from drafttests import test_base -class DraftOCA(unittest.TestCase): +class DraftOCA(test_base.DraftTestCaseDoc): """Test reading and writing of OCA files with Draft.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - self.doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != self.doc_name: - App.newDocument(self.doc_name) - else: - App.newDocument(self.doc_name) - App.setActiveDocument(self.doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc_name)) - def test_read_oca(self): """Read an OCA file and import its elements as Draft objects.""" operation = "importOCA.import" @@ -87,11 +71,4 @@ class DraftOCA(unittest.TestCase): obj = Draft.export_oca(out_file) self.assertTrue(obj, "'{}' failed".format(operation)) - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc_name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_pivy.py b/src/Mod/Draft/drafttests/test_pivy.py index 2eef150b4f..b2f10f0eca 100644 --- a/src/Mod/Draft/drafttests/test_pivy.py +++ b/src/Mod/Draft/drafttests/test_pivy.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -28,35 +29,16 @@ ## \addtogroup drafttests # @{ -import unittest - -import FreeCAD as App import FreeCADGui as Gui import drafttests.auxiliary as aux from draftutils.messages import _msg +from drafttests import test_base -class DraftPivy(unittest.TestCase): +class DraftPivy(test_base.DraftTestCaseDoc): """Test for the presence of Pivy and that it works with Coin3D.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - self.doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != self.doc_name: - App.newDocument(self.doc_name) - else: - App.newDocument(self.doc_name) - App.setActiveDocument(self.doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc_name)) - def test_pivy_import(self): """Import Coin (Pivy).""" module = "pivy.coin" @@ -68,15 +50,8 @@ class DraftPivy(unittest.TestCase): import pivy.coin as coin cube = coin.SoCube() _msg(" Draw cube") - Gui.ActiveDocument.ActiveView.getSceneGraph().addChild(cube) + Gui.getDocument(self.doc).ActiveView.getSceneGraph().addChild(cube) _msg(" Adding cube to the active view scene") self.assertTrue(cube, "Pivy is not working properly.") - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc_name) - ## @} diff --git a/src/Mod/Draft/drafttests/test_svg.py b/src/Mod/Draft/drafttests/test_svg.py index 5ff9709b67..88e77e3388 100644 --- a/src/Mod/Draft/drafttests/test_svg.py +++ b/src/Mod/Draft/drafttests/test_svg.py @@ -1,6 +1,7 @@ # *************************************************************************** # * Copyright (c) 2013 Yorik van Havre * # * Copyright (c) 2019 Eliud Cabrera Castillo * +# * Copyright (c) 2025 FreeCAD Project Association * # * * # * This file is part of the FreeCAD CAx development system. * # * * @@ -36,6 +37,7 @@ import Draft import drafttests.auxiliary as aux from draftutils.messages import _msg +from drafttests import test_base try: import Arch @@ -45,26 +47,9 @@ else: have_arch = True -class DraftSVG(unittest.TestCase): +class DraftSVG(test_base.DraftTestCaseDoc): """Test reading and writing of SVGs with Draft.""" - def setUp(self): - """Set up a new document to hold the tests. - - This is executed before every test, so we create a document - to hold the objects. - """ - aux.draw_header() - self.doc_name = self.__class__.__name__ - if App.ActiveDocument: - if App.ActiveDocument.Name != self.doc_name: - App.newDocument(self.doc_name) - else: - App.newDocument(self.doc_name) - App.setActiveDocument(self.doc_name) - self.doc = App.ActiveDocument - _msg(" Temporary document '{}'".format(self.doc_name)) - def test_read_svg(self): """Read an SVG file and import its elements as Draft objects.""" operation = "importSVG.import" @@ -101,11 +86,11 @@ class DraftSVG(unittest.TestCase): import Draft sb = Part.makeBox(1,1,1) - b = App.ActiveDocument.addObject('Part::Feature','Box') + b = self.doc.addObject('Part::Feature','Box') b.Shape = sb s = Arch.makeSpace(b) - App.ActiveDocument.recompute() + self.doc.recompute() try: Draft.get_svg(s, direction=App.Vector(0,0,0)) @@ -116,11 +101,4 @@ class DraftSVG(unittest.TestCase): else: self.fail("no exception thrown") - def tearDown(self): - """Finish the test. - - This is executed after each test, so we close the document. - """ - App.closeDocument(self.doc_name) - ## @}