Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

visual studio - VSCode pytest test discovery fails when debugging Django tests

I'm trying to debug my first django test, but VSCode is returning: Ran 0 tests in 0.000s. On the other hand, when I use an integrated git bash (I'm using a windows OS) in VSCode terminals or externally, it returns: Ran 1 test in 0.0014s. It also echoes FAILED and a traceback for a valid error within the test.

My vscode launch.json:

{   
    ...
    "configurations": [
        ...
        {
            "name": "Django Tests",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\testprojectname\manage.py",
            "args": [
                "test"
            ],
            "django": true,
        }
    ]
}

My vscode settings.json:

{
    "python.pythonPath": "C:\Users\user\.virtualenvs\backend-Vg-KBKTA\Scripts\python.exe",
    "python.testing.pytestEnabled": true,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": false,
    "python.testing.pytestArgs": [
        "testprojectname"
    ]
}
  • Note that C:Usersuser.virtualenvsackend-Vg-KBKTAScriptspython.exe is a valid path to the pipenv shell [(1) it is echoed when I type in the bash: pipenv --venv (2) it works when I launch the django debugger]

So far, I've tried:

  1. Updating my pytest according to VSCode pytest test discovery fails
  2. Changing around the name of my tests.py file to test_example.py according to Pytest - no tests ran
  3. Changing the class of the tests to prefix with Test_ according to the same link in (2)

This is what the test currently looks like


class Test_Contacts(unittest.TestCase):

    def test_create_contact_message(self):
        """Create a new contact message."""
        url = reverse('message-list')
        data = {
            'first_name': 'DabApps',
            'last_name': 'jack',
            'email': '[email protected]',
            'message': 'hi, how are you?',
            'message_type': 1
        }
        # this is just random code to cause an error
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Message.objects.count(), 1)
        self.assertEqual(Message.objects.get().name, 'DabApps')
question from:https://stackoverflow.com/questions/65836441/vscode-pytest-test-discovery-fails-when-debugging-django-tests

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was not able to solve the original problem, but I found a good workaround. I can now see 'debug test' above the test code. I'm not sure why, but it may be because I installed pytest-django or the extension python test explorer ui.

python test explorer ui story

  1. I googled for ways to debug django tests and eventually found a stackoverflow comment that led me to this https://github.com/Microsoft/vscode-python/issues/3911
  2. That github issue mentioned an adapter, which led me to downloading the VS code extension by little fox team: "Python Test Explorer for Visual Studio Code" with 262,853 downloads as of 2021 Jan 24th.
  3. now when I ran pytest --collect-only in the git bash, I ran into the error: apps aren't loaded yet. This led me to where I first found out about pytest-django "Apps aren't loaded yet" when trying to run pytest-django

pytest-django story

  1. read pytest-django documentation
  2. made pytest.ini file with the following content:
[pytest]
DJANGO_SETTINGS_MODULE = testprojectname.settings
python_files = tests.py test_*.py *_tests.py
  • testprojectname is the name of the django project
  1. now I noticed that the test I made in tests.py had clickable words for debugging above it. Clicking debug test took a while to initiate, but it allowed the code to stop at the red dot VS code breakpoints. clickable words for debugging above test

  2. Although that was all I needed, I also noticed that I can now run the tests from the VS code test explorer, so I no longer have to use pipenv shell in the bash to run all the tests. VS code test explorer found test


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...