In VS Code there is a tab called testing which I want to reconfigure as I mistakenly configured it in wrong way while initially configuring it.
My current settings.json
file:
{
"window.zoomLevel": 3,
"workbench.iconTheme": "material-icon-theme",
}
I had a Django-based project for which I was creating the tests using pytest. For running those tests I went to configure the test section of VS Code.
example of test functions i wrote
While configuring the testing section of VS Code I chose the wrong directory, because of which I was not able to see my test class and functions to run the tests.
testing section of vscode not showing my test classes and functions due to wrong directory chose while configuration
Hence, I wanted to reset or reconfigure the testing section of vscode so that I can select the root directory of the project which should let me see the test classes and functions.
My pytest based testing classes and functions:
import pytest
from django.contrib.auth.models import User
from rest_framework import status
@pytest.mark.django_db
class TestCreateCollection:
def test_if_user_annoymous_returns_401(self, api_client):
response = api_client.post('/store/collections/', {'title':'a'})
assert response.status_code == status.HTTP_401_UNAUTHORIZED # type: ignore
def test_if_user_not_admin_returns_403(self, api_client):
api_client.force_authenticate(user=User(is_staff=False))
response = api_client.post('/store/collections/', {'title':'a'})
assert response.status_code == status.HTTP_403_FORBIDDEN # type: ignore
def test_if_data_is_invalid_returns_400(self, api_client):
api_client.force_authenticate(user=User(is_staff=True))
response = api_client.post('/store/collections/', {'title':''})
assert response.status_code == status.HTTP_400_BAD_REQUEST # type: ignore
assert response.data['title'] is not None # type: ignore
def test_if_data_is_valid_returns_201(self, api_client):
api_client.force_authenticate(user=User(is_staff=True))
response = api_client.post('/store/collections/', {'title':'a'})
assert response.status_code == status.HTTP_201_CREATED # type: ignore
assert response.data['id'] > 0 # type: ignore
Ravi Yadav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If you have the icon in the left sidebar for Testing,it may be because you have installed the extension Python.
I also set up a wrong configuration (unittest instead of pytest, in an incorrect folder).
After that I was unable to change again the settings (initial configuration GUI in VScode, that allow you to introduce the paths).
For me what it worked was:
- Reinstalling the Extension (after uninstalling it)
2. Later:
-
2.1. Click into VScode “Settings”
-
2.2. Search in the text_bar “python.test”.
-
2.3 This will display all the features of python testing. I will go through all of them and click on the gear that appears when you hover over it. And click on “Reset setting”. (If you see, I work in the tab “workspace” but maybe you will have to reset it in the other tabs too (“USer” or “remote” tabs)
-
2.4 Later close the project, open it again. And when you click again in the testing icon
It will ask you again about the configuration info.
It did not recognized my tests yet (pytest configuration).
So:
- This was because I needed to add a “init.py” file in the folder with the tests that were in subfolders, as described in the official Vscode documentation:
4 . I also needed to modify the “.vscode/settings.json” file of your project
And add those 2 lines
NOTE:
I am working with Conda environment in Remote, Linux, and Python 3.9.
Based on these tutorials official and unofficial I figured it out and now it works great!