1

My use-case is:

  1. I have a set of python scripts in one folder named 'Source'.
  2. In another folder named 'Test Scripts', I have a set of dedicated test-case scripts (using PyTest) for each python script. I am able to run the test-cases.
  3. Both the folders have init python script.

Problem: Temporary files are getting created during test-case run. I tried using a fixture to resolve this issue. Particularly, I tried following this SO question, PyTest: Auto delete temporary directory created with tmpdir_factory But, it didn't help me achieve my objective.

Moreover, I was wondering as if I can give python dict as a command-line argument because scripts in source folder run through command with the dictionary as an argument. Currently, I pass this command,

python -m pytest Test Scripts / test_script.py

Code Snippet of one of the test-case:

import pytest
from script_name import class_name
import shutil

@pytest.fixture
def obj_creation():
    my_obj = class_name(data = " { 'a': 'b','c': 'd'} ")
    return [my_obj]

@pytest.fixture(scope='session')
def project_file(tmpdir_factory):
    my_tmpdir = tmpdir_factory.mktemp("data")
    yield my_tmpdir 
    shutil.rmtree(str(my_tmpdir))

def test_no_new_office_ID(project_file, obj_creation):
    x = obj_creation[0].ofc_email()
    assert not x

I don't find any web material which is complete (with all possible use-cases). Please help me with this.

Snapshot: enter image description here

Sarvesh Pandey
  • 322
  • 7
  • 17
  • 2
    Your fixture looks ok to me, provided that all temp files indeed are created in the directory returned by `project_file` (which cannot be seen in the shown code). What is your concrete problem? Who is actually responsible for creating the temp files? – MrBean Bremen Jun 05 '20 at 08:27
  • When I run any of the test script, temp files get created. That gives me an impression that I am not utilizing fixture properly. Now, I have included a snapshot for better clarity. It could be because of importing a source script. – Sarvesh Pandey Jun 05 '20 at 08:49
  • 1
    These are not temp files - these are [compiled Python files](https://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files), that are needed during execution. You just have to make sure they are included in .gitignore or such, but you shouldn't remove them. – MrBean Bremen Jun 05 '20 at 08:55
  • I wasn't aware of and quickly googled about .gitignore Can you please clarify that how can I use this as it's certainly not good to commit these files to repository. – Sarvesh Pandey Jun 05 '20 at 09:02
  • 1
    Just add a `*.pyc` line into your `.gitignore`, so these files will be ignored. Check the [documentation](https://git-scm.com/docs/gitignore). – MrBean Bremen Jun 05 '20 at 09:04
  • That solved my purpose. One last thing, is it like I have to push .gitignore file in my remote repository or is there any way to keep it in my local repository. Thanks a lot! – Sarvesh Pandey Jun 05 '20 at 09:19
  • 1
    They are usually added to the repo (as mentioned in the documentation), because you want to use them in every clone of the repo. – MrBean Bremen Jun 05 '20 at 09:21

1 Answers1

0

I'm using Django's TestCase class for automatic tests. Here is the code I'm using to delete temporary files which were created in the tests:

import shutil


@classmethod
def tearDownClass(cls):
    try:
        shutil.rmtree(django_settings.TESTS_MEDIA_ROOT)
    except OSError:
        pass
    return super().tearDownClass()

Maybe you can use something similar to delete your files?

Uri
  • 2,992
  • 8
  • 43
  • 86