PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 9, 2022

[FIXED] How do I create an automated test for my python script?

 October 09, 2022     continuous-integration, gitlab, python, testing, unit-testing     No comments   

Issue

I am fairly new to programming and currently working on a python script. It is supposed to gather all the files and directories that are given as paths inside the program and copy them to a new location that the user can choose as an input.

import shutil 
import os 
from pathlib import Path 
import argparse 
 
src = [  [insert name of destination directory, insert path of file/directory that  
          should be copied ]
      ]

x = input("Please choose a destination path\n>>>")
if not os.path.exists(x):
    os.makedirs(x)
    print("Directory was created")
else:
    print("Existing directory was chosen")

dest = Path(x.strip())

for pfad in src:
    
    if os.path.isdir(pfad[1]):          
        shutil.copytree(pfad[1], dest / pfad[0]) 
    
    elif os.path.isfile(pfad[1]): 
        pfad1 = Path(dest / pfad[0])
        if not os.path.exists(pfad1):
             os.makedirs(pfad1)
        
        shutil.copy(pfad[1], dest / pfad[0]) 

    else:
        print("An error occured")
        print(pfad) 

print("All files and directories have been copied!")
input()

The script itself is working just fine. The problem is that I want write a test that automatically test the code each time I push it to my GitLab repository. I have been browsing through the web for quite some time now but wasnt able to find a good explanation on how to approach creating a test for a script like this. I would be extremely thankful for any kind of feedback or hints to helpful resources.


Solution

First, you should write a test that you can run in command line. I suggest you use the argparse module to pass source and destination directories, so that you can run thescript.py source_dir dest_dir without human interaction.

Then, as you have a test you can run, you need to add a .gitlab-ci.yml to the root of the project so that you can use the gitlab CI. If you never used the gitlab CI, you need to start here: https://docs.gitlab.com/ee/ci/quick_start/

After that, you'll be able to add a job to your .gitlab-ci.yml, so that a runner with python installed will run the test. If you don't understad the bold terms of the previous sentence, you need to understant Gitlab CI first.



Answered By - ofaurax
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing