mardi 2 février 2021

Is it a good practice to include some data for doing a test?

I have never did unit testing or integration testing and would like to learn the good way (but keeping things simple since I am a newcomer). Here is my problem: I want to offer a test for a function that manipulates and exports some files (that unzip: imbricated zip file). Should I provide a minimal data example with the test (as I explain after)? If not what would you do?

I created:

  • a "tests" directory inside my utilities subpackage
  • a "data" directory in my tests subpackage
  • a dummy (almost empty for test purpose) zip file called zip_root.zip and structured as follow: {zip_root.zip:{zip_child1:{zip_child1_grandchild1:file_child1_grandchild.txt},zip_child2:file_child2.txt} in "tests/data/input/zip" directory
  • a "tests/data/output/unzip" empty directory
  • a test file "test_extract.py" in tests subpackage
  • the target module "extract_zip.py"

Below are the target module "extract_zip.py" and test file code (if needed and allowed by SO I can provide the whole package).

extract_zip.py:

'''
walk a file tree from specified starting point, unzip any zip files encountered in the
directory where zip file was found, and then delete the zip file after succesfully
unzipping
'''
import zipfile, os, glob
import shutil
 
def extract_zip(zippedfilepath,toFolder):
    with zipfile.ZipFile(zippedfilepath, 'r') as zfile:
        #print("Extracting {}".format(zippedfilepath))
        zfile.extractall(path=toFolder)
 
def extract_nested_zip(zipped_file, toFolder):
    """ Extract a zip file including any nested zip files
        Delete the zip file(s) after extraction
    """
    extract_zip(zipped_file,toFolder)
    basename = os.path.basename(zipped_file).split('.zip')[0]
    path = list(glob.iglob(os.path.join(toFolder,basename,'**/*.zip'), recursive=True))
    for filepath in path:
        extract_nested_zip(filepath, os.path.dirname(filepath))
        os.remove(filepath)
 
if __name__ == "__main__":
    rootpath = r"datalib/utilities/tests/data"
    zippedFile = os.path.join(rootpath,r'input/zip/zip_root.zip')
    toFolder= os.path.join(rootpath,r'output/unzip')
    extract_nested_zip(zippedFile, toFolder)

test_extract_zip.py

from ..extract_zip import extract_zip
import os
import tempfile
 
def test_extract_zip():
    rootpath = r"datalib\utilities\tests\data"
    zippedfilepath = os.path.join(rootpath,r"input\zip\zip_root.zip")
    with tempfile.TemporaryDirectory() as tmpdir:
        toFolder =tmpdir
        extract_zip(zippedfilepath,toFolder)
        #import pdb;pdb.set_trace()
        assert os.listdir(toFolder)==['zip_root']

Here is an image of the hierarchy to clear the organization: enter image description here

Remark: I am not familiar with simple test practice. I used to make my test inside the targeted module below the if __name__ = "__main__" but I am not sure it is really good practice.

Aucun commentaire:

Enregistrer un commentaire