Test and Code: 87: Paths to Parametrization – from one test to many

There’s a cool feature of pytest called parametrization.
It’s totally one of the superpowers of pytest.

It’s actually a handful of features, and there are a few ways to approach it.
Parametrization is the ability to take one test, and send lots of different input datasets into the code under test, and maybe even have different output checks, all within the same test that you developed in the simple test case.

Super powerful, but something since there’s a few approaches to it, a tad tricky to get the hang of.

Sponsored By:

Support Test & Code – Python Testing & Development

Links:

<p>There&#39;s a cool feature of pytest called parametrization.<br> It&#39;s totally one of the superpowers of pytest.</p> <p>It&#39;s actually a handful of features, and there are a few ways to approach it.<br> Parametrization is the ability to take one test, and send lots of different input datasets into the code under test, and maybe even have different output checks, all within the same test that you developed in the simple test case.</p> <p>Super powerful, but something since there&#39;s a few approaches to it, a tad tricky to get the hang of.</p><p>Sponsored By:</p><ul><li><a href=”https://testandcode.com/pycharm” rel=”nofollow”>PyCharm Professional</a>: <a href=”https://testandcode.com/pycharm” rel=”nofollow”>Try PyCharm Pro for 4 months. Offer good through June 10. Try out Pro features like integrated coverage and profiling, and extended support for Django, Flask, Pyramid, Cython, and more.</a> Promo Code: TESTNCODE2019</li></ul><p><a href=”https://www.patreon.com/testpodcast” rel=”payment”>Support Test & Code – Python Testing & Development</a></p><p>Links:</p><ul><li><a href=”https://docs.pytest.org/en/latest/changelog.html” title=”pytest changelog” rel=”nofollow”>pytest changelog</a></li><li><a href=”https://docs.pytest.org/en/latest/deprecations.html#deprecations” title=”pytest deprecations and removals” rel=”nofollow”>pytest deprecations and removals</a></li><li><a href=”https://pragprog.com/book/bopytest/python-testing-with-pytest” title=”Python Testing with pytest” rel=”nofollow”>Python Testing with pytest</a> &mdash; Test function parametrization is in chapter 2. Fixture parametrization is in chapter 3.</li><li><a href=”https://docs.pytest.org/en/latest/parametrize.html” title=”Parametrizing test functions — pytest documentation” rel=”nofollow”>Parametrizing test functions — pytest documentation</a></li><li><a href=”https://docs.pytest.org/en/latest/fixture.html#fixture-parametrize” title=”pytest fixtures — pytest documentation” rel=”nofollow”>pytest fixtures — pytest documentation</a></li></ul>
Planet Python

codingdirectional: Tell the user how many duplicate files have the python program deleted

Welcome back again, in this chapter we will create a message box to tell the user how many duplicate files have been deleted from each selected file. The game plan here is simple, we will attach a message variable to the label widget and pass that widget to each remove thread instance so the program can modify the content of that message by adding in the selected file name as well as the total number of duplicate files that have been remove from the selected folder. That modify message will then be shown by the messagebox on the main thread at the end of each file deleting process. The first file we need to edit is the main file where we will pass in the tkinter’s label object with the message variable to each new remove thread instance which the program has created, the remove thread instance will then modify the content of that same message which includes the total number of duplicate files that have been deleted together with the file name of the original selected file. We need not worry too much about whether those threads will compete with each other to modify the content of the message or not because one process will finish before another so therefore there is always a time gap in between the process in this case. Here is the new version of the main file.

 from tkinter import * from tkinter import filedialog from Remove import Remove import os from tkinter import messagebox  win = Tk() # 1 Create instance win.title("Multitas") # 2 Add a title win.resizable(0, 0) # 3 Disable resizing the GUI win.configure(background='black') # 4 change background color  # 5 Create a label aLabel = Label(win, text="Remove duplicate", anchor="center") aLabel.grid(column=0, row=1) aLabel.configure(foreground="white") aLabel.configure(background="black") aLabel.message = ''  # 6 Create a selectFile function to be used by button def selectFile():      fullfilenames = filedialog.askopenfilenames(initialdir="/", title="Select a file") # select multiple files from the hard drive      if(fullfilenames != ''):          fullfilenamelist = win.tk.splitlist(fullfilenames)         directory_path = os.path.dirname(os.path.abspath(fullfilenamelist[0]))          os.chdir(directory_path)  # change the directory to the selected file directory          folder = filedialog.askdirectory()  # 7 open a folder then create and start a new remove thread to delete the duplicate file         folder = folder.replace('/', '\')  # 8 this is for the windows separator only          if(folder != '' and folder != os.getcwd()):              for fullfilename in fullfilenamelist:                  if(fullfilename != ''):                     filename = fullfilename.split('/')[-1]                     remove = Remove(folder, filename, fullfilename, directory_path, aLabel)                     remove.start()                     remove.join()                     messagebox.showinfo('Remove duplicate files', aLabel.message)          else:             messagebox.showinfo("Error", "Kindly select one folder and it must be a different one")             return      else:         messagebox.showinfo("Select file", "You need to select a file!")         return   # 9 Adding a Button action = Button(win, text="Select File", command=selectFile) action.grid(column=0, row=0) action.configure(background='brown') action.configure(foreground='white')  win.mainloop()  # 10 start GUI 

And here is the edit version of the remove thread class.

 import threading import os import filecmp from tkinter import messagebox  class Remove(threading.Thread):      def __init__(self, massage,  filename, fullfilename, directory_path, aLabel):          threading.Thread.__init__(self)         self.massage = massage         self.filename, self.file_extension = os.path.splitext(filename)         self.fullfilename = fullfilename         self.directory_path = directory_path         self.count = 0         self.aLabel = aLabel      def run(self):          filepaths = os.listdir(self.massage)          for filepath in list(filepaths):             os.chdir(self.massage)             if(os.getcwd() != self.directory_path): # make sure that we will not delete the same file in the selected file directory                 if(os.path.isfile(filepath)):                     filename, file_extension = os.path.splitext(filepath)                     self.remove_file(file_extension, filepath)                 else:                     self.delete_duplicate(os.path.join(self.massage, filepath))             else:                 continue          self.aLabel.message = 'Removed ' + str(self.count) + ' duplicate : ' + self.filename # show this message box each time a set of duplicate files have been remove       def delete_duplicate(self, folder): # sub method to pass folder to          filepaths = os.listdir(folder)          for filepath in list(filepaths):             os.chdir(folder)             if(os.getcwd() != self.directory_path):                  if(os.path.isfile(filepath)):                     filename, file_extension = os.path.splitext(filepath)                     self.remove_file(file_extension, filepath)                 else:                     self.delete_duplicate(os.path.join(folder, filepath))              else:                 continue      def remove_file(self, file_extension, filepath):         if (file_extension == self.file_extension):             if filecmp.cmp(filepath, self.fullfilename, shallow=False):                 os.remove(filepath)                 self.count += 1 

Now each remove thread will manage it’s own business so there is no conflict with the count variable and the old message text will be replaced by the new one by the next process which changes it. That is basically it for this remove duplicate project, we just need to do some decorations on the user interface in the next chapter then we are ready to pack it up and use it on our windows’ desktop.

Planet Python