How to get PyTest working in Visual Studio
Asked Answered
A

4

32

I want to integrate PyTest with Visual Studio so my tests show up in the Test Explorer. So far I have not found any way to do this while some old posts here suggest that people have done it before. As far as I can tell, as this article suggests, someone should develop an adapter interface for PyTest. However, other posts like this show that others were successful in getting this to work. But these two have not worked for me so far. Is there a way to get this working?

Aguilar answered 17/2, 2018 at 3:18 Comment(6)
Did you try this: donjayamanne.github.io/pythonVSCodeDocs/docs/unittests ?Sargassum
That is for VSCode which is a different product that Visual StudioAguilar
github.com/Microsoft/PTVS/issues/1847Hunkydory
I am not sure if it helps, but you can produce test results from py.test tests in junit format and I noticed there is junit runner/wrapper for visual studioApothecium
Whats the project type you are work in , is it Flask, Django or Just Pure python ?Aurar
In Visual Studio 2017 -- I noticed that I had to install python3.6 and then have it install using the requirements.txt file, which installs the pytest plugin. Somehow python3.9 has some restrictions, hence the need for going back to python3.6 and get the pytest plugin to work.Zingale
I
4

Support for Pytest in Visual Studio has been added on Visual Studio 2019 (16.3 Preview 2)

You have to change your project's test framework by right-clicking it and going to Properties -> Test

You can add a pytest.ini to your project to configure pytest further.

More info from MS themselves: https://devblogs.microsoft.com/python/whats-new-for-python-in-visual-studio-16-3-preview-2/

Ingate answered 3/9, 2019 at 5:45 Comment(0)
K
1

first you have to Ensure that pytest has been installed in the currently configured python environment. And then you have to configure it like following: Command line options for the pytest can be passed by adding the necessary options into the following section in the user or workspace settings file settings.json: Add the options as individual items as shown below:

"python.unitTest.pyTestArgs": [
    "--exitfirst",
    "--verbose"
],

Plz tell me if it is working well

Klingensmith answered 21/6, 2019 at 13:41 Comment(0)
D
0

First install Pytes in your local machine. If your visula studio is already integrated with Python and if your test is also used Pytest then you dont need to worry,your visual studion will use Pytest.

Disharmonious answered 24/8, 2019 at 16:9 Comment(0)
F
0

Per the other answers, make sure you have the PyTest package installed.

I found that if I had created a project before I installed PyTest, I needed to go into those project's .pyproj file and add the following lines before </PropertyGroup> :

<TestFramework>Pytest</TestFramework>
<UnitTestPattern>test*.py</UnitTestPattern>
<UnitTestRootDirectory>.</UnitTestRootDirectory>

and save the changes. Test Explorer was then able to automatically detect the unit test files when the filename started with "test", and the unit test function names started with "test", e.g. filename: test_flowCalc.py , containing a unit test such as: def test_freeze_temp_calc():

I also found that Visual Studio scans for the test files and test functions every couple of seconds, so if you're creating unit tests, they won't appear in the Test Explorer list until you save the unit test(s). Sounds obvious, but most folks reading this are probably new to PyTest and may have little experience with Visual Studio, so trying to manage expectations.

Additionally, Visual Studio won't display tests if there are runtime errors in the file containing the method(s) you're trying to test.

Fiacre answered 3/3 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.