jeudi 21 janvier 2021

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:\Users\user\.virtualenvs\backend-Vg-KBKTA\Scripts\python.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': 'giehogho24h@gmo8y9tgail.com',
            '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')

Aucun commentaire:

Enregistrer un commentaire