Test Files Creating a Temporal Directory in Python Unittests
Overview
Some Python projects need to work with files and directories, to check if the contents are written to a file or that the contents are written as expected.
To easily unit test creating, deleting and checking their contents, we can create a temporal folder that will contain all of the above generated files and work with them.
Code
In our test code, we use the tempfile
module.
This module creates temporary files and directories.
We can choose between high-level and lower-level interfaces depending on the type of test we want, main difference is that the former one handles all directories and its content removal as soon as the completion of the context or the destruction of the temporary directory object.
high-level interface
Using tempfile.TemporaryDirectory
to create a temporal directory (or
tempfile.TemporaryFile
for files)
before each method:
import tempfile
class TestExample(unittest.TestCase):
def setUp(self):
# Create a temporary directory
self.test_dir = tempfile.TemporaryFile()
def tearDown(self):
# Close the file, the directory will be removed after the test
self.test_dir.close()
if __name__ == '__main__':
unittest.main()
Using in single test
Create a temporary directory using the context manager:
import tempfile
class TestExample(unittest.TestCase):
def test_example(self):
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
if __name__ == '__main__':
unittest.main()
References
- Solve Selenium WebDriverException executable needs to be in PATH error messageApril 24, 2020
- Pip upgrade all packages at once with a one-liner command January 31, 2019
- Test Files Creating a Temporal Directory in Python Unittests
- How to Translate a Python Project With Gettext the Easy WayAugust 29, 2018
Behave Testing
Django webframework
- August 1, 2023
- How to create a reusable Django app and distribute it with PIP or publish to pypi.orgJune 29, 2021
- How To Serve Multiple Django Applications with uWSGI and Nginx in Ubuntu 20.04October 26, 2020
- How to add favicon to Django in 4 stepsSeptember 3, 2020
- Categories in Django with BreadcrumbsAugust 30, 2020
- How To Migrate From SQLite To PostgreSQL In Django In 3 stepsAugust 28, 2020
- Practical guide to internationalize a Django app in 5 steps.August 24, 2020
- Disable new users singup when using Django's allauth packageSeptember 3, 2019
- How to add ads.txt to Django as requested by Google AdsenseAugust 30, 2019
- Have multiple submit buttons for the same Django formJuly 2, 2019
- Better Testing with Page Object Design in DjangoMay 1, 2019
- Generating slugs automatically in Django without packages - Two easy and solid approachesFebruary 14, 2019
- How to set up Django tests to use a free PostgreSQL database in HerokuFebruary 13, 2019
- Dynamically adding forms to a Django FormSet with an add button using jQueryFebruary 6, 2019
- Use of Django's static templatetag in css file to set a background imageFebruary 1, 2019
- Activate Django's manage.py commands completion in Bash in 2 stepsJanuary 29, 2019
- Sending Emails with Django using SendGrid in 3 easy stepsJanuary 9, 2019
- Adding Users to Your Django Project With A Custom User ModelSeptember 21, 2018
- Setting Up A Factory For One To Many Relationships In FactoryboyApril 17, 2018
- Generate UML class diagrams from django modelsMarch 24, 2018
- Set Up Ubuntu To Serve A Django Website Step By StepJuly 3, 2017
- Django Project Directory StructureJuly 16, 2016
- How to Have Different Django Settings for Development and Production, and environment isolationJune 10, 2016
- Django OverviewJune 2, 2016
Django Forms
- Adding a Cancel button in Django class-based views, editing views and formsJuly 15, 2019
- Using Django Model Primary Key in Custom Forms THE RIGHT WAYJuly 13, 2019
- Django formset handling with class based views, custom errors and validationJuly 4, 2019
- How To Use Bootstrap 4 In Django FormsMay 25, 2018
- Understanding Django FormsApril 30, 2018
- How To Create A Form In DjangoJuly 29, 2016
Flask web microframework
- Understanding Flask's context conceptJanuary 26, 2017
- Avoid Using Flask Instance Folder When Deploying To HerokuJanuary 24, 2017
- Managing Environment Configuration Variables In Flask With DotenvJanuary 24, 2017
- Organize A Flask Project To Handle Production And Development Environments EffectivelyJanuary 11, 2017
- An Overview Of Flask Main Concepts And How It WorksDecember 31, 2016
Python Language
- Python Tools To Write Better CodeNovember 7, 2017
- Python Language Main Concepts And SummaryJune 11, 2017
- Python notesMay 30, 2016
Python Language Concepts
- Understanding How Python Packages Modules And Imports WorkJanuary 2, 2017
- Python Language Basic ConceptsJune 14, 2016
Python Environment
- Understanding Python 3 virtual environments different approachesJanuary 15, 2019
- Python Projects Isolation Using Virtual EnvironmentsJune 10, 2016
Articles
Subcategories
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·