Python Forum
Remove function and unit test
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove function and unit test
#1
Hello everyone,

let's say I want to create a 'utils' package with several functions
working on files and folders. I want to do it once properly so I can use
them in all my personal projects without feeling to re invent the wheel
each time.
I also want to learn 2 items by the way:
- exception handling, already read a lot about EAFP vs LBYL, I think the
following function suits well to EAFP.
- unit tests with pytest tool, it seems simple enough to start with.

My function is:

def removeanything(src):
			"""
			remove files or folders
			"""
			try:
				os.remove(src)
				print('File is removed')
			except IsADirectoryError:
				shutil.rmtree(src)
				print('Folder is removed')
			except FileNotFoundError:
				print('File/folder is not existing')
			except PermissionError:
				print('Not allowed to suppress this file')


Regarding the function it self:
do you think it is worth handling each possible exception or should I just
handle IsADirectoryError for folder removing and all the other one in a unique
one?
What would be the interest to raise an exception in that case to be caught by the caller?
Are there some exceptions I didn't handle and that could possibly occur?

Regarding the testing:
I need to test whether a file or a folder is well removed and the exception are
well handled:
- file exists and permissions are ok => file is removed
- folder exists and permissions are ok => folder is removed
- neither file or folder exist => FileNOtFoundError
- permissions issues => PermissionError
My main concern is to create the conditions of the test (files/folders to be
removed with good permissions) and to remove them in a clean way once test is over
or has been interrupted.
What is the skeletton of those steps?

Sorry for this long post.

Thanks by adavnce for your answers.
Reply


Messages In This Thread
Remove function and unit test - by ftg - Jan-07-2020, 09:28 AM
RE: Remove function and unit test - by ndc85430 - Jan-07-2020, 10:00 AM
RE: Remove function and unit test - by Gribouillis - Jan-07-2020, 10:34 AM
RE: Remove function and unit test - by ftg - Jan-07-2020, 12:54 PM
RE: Remove function and unit test - by ndc85430 - Jan-07-2020, 03:10 PM
RE: Remove function and unit test - by ftg - Jan-07-2020, 02:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 482 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,219 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Using Dictionary to Test Evenness of Distribution Generated by Randint Function new_coder_231013 6 3,362 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013
  Writing unit test results into a text file ateestructural 3 4,825 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Test a class function via "unittest " Penguin827 1 1,646 Jul-10-2020, 08:31 AM
Last Post: Gribouillis
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,164 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,594 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  Odd Unit Test Behavior ichabod801 3 2,630 Jan-02-2020, 03:34 PM
Last Post: ichabod801
  Define unit of measure of a number doug2019 3 2,428 Oct-15-2019, 03:43 PM
Last Post: jefsummers
  Unit testing - AssertRaises kerzol81 3 4,647 Oct-07-2019, 10:35 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020