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
#2
I'm glad you're looking into unit testing. I've not used pytest, but I suspect it has functionality to mark a function to be run before tests and after tests so you can do the setting up and clearing up in that way (it's common to want to do that, so I'd be surprised if pytest didn't have such facilities).

As for creating the files and folders in the first place, take a look at this - the tempfile module has facilities to create temporary files. If you're on Unix, then look at the os module (not listed in the link because its scope is more general than just the file system) which has a chmod function for setting permissions (I don't know what one would do on Windows I'm afraid!).
Reply
#3
If you are in linux, you can very easily create a ram disk where you would store the temporary files and directories needed for the tests. It would make the tests run much faster and also avoid unnecessary disk accesses. Here is a recipe to create a ramdisk of size 512MB
Output:
mkdir /media/ramdisk chmod 777 /media/ramdisk sudo mount -t tmpfs -o size=512M tmpfs /media/ramdisk
It can also be automounted at boot time by adding this line to /etc/fstab
Output:
none /media/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=512M 0 0
Then create the necessary files and directories for the tests as subdirectories of /media/ramdisk
Reply
#4
Thanks for the answer.

I am indeed under linux OS.
Your solution means an additional step in order to drive the tests.
Intuitively, I would say that I prefer the test to remain "under" python control.

By the way if I use tempfile.TemporaryFile context manager, will I be able to remove the files with my function during all the time it is activated?
Reply
#5
Btw I have found this feature from pytest:
https://pytest.org/en/latest/tmpdir.html...r-handling
Reply
#6
(Jan-07-2020, 12:54 PM)ftg Wrote: Intuitively, I would say that I prefer the test to remain "under" python control.

I think that's a good approach. Having the extra step somewhere else means it's a bit hidden and it's easy to forget about it.

Quote:By the way if I use tempfile.TemporaryFile context manager, will I be able to remove the files with my function during all the time it is activated?

I'm not sure what you mean really. Even without the context manager, you can create and remove the files in the set up/tear down steps.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 379 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,064 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,199 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013
  Writing unit test results into a text file ateestructural 3 4,653 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Test a class function via "unittest " Penguin827 1 1,576 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,062 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,511 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  Odd Unit Test Behavior ichabod801 3 2,541 Jan-02-2020, 03:34 PM
Last Post: ichabod801
  Define unit of measure of a number doug2019 3 2,344 Oct-15-2019, 03:43 PM
Last Post: jefsummers
  Unit testing - AssertRaises kerzol81 3 4,496 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