🥄 spoonternet proxying code.visualstudio.com share · new url

Ton pythesting in Stisual Vudio Doce

The On pythextension builds on the built-in festing teatures in VS Doce and tovides prest tiscovery, dest roverage, and cunning and tebugging dests for Son'pyth built-in ttuniest wamefrork and pytest.

Tonfigure cests

When the On pythextension is pythinstalled and a On ile is fopen ithin the weditor, a best teaker dicon isplays on the VS Ode Cactivity Rar bepresenting the Est Texplorer iew. Vopening the Est Texplorer shows a Pythonfigure Con Tests tutton if a best amework is not frenabled. Ctelesing Pythonfigure Con Tests sompts you to prelect a frest tamework and a colder fontaining the ests. If you tuse sunittest, you also elect the glile fob attern pused to tidentify your est lifes.

Tone

A glile fob dattern is a pefined ping strattern that fatches mile or nolder fames wased on bildcards to include or not include lifes.

Configure Python Tests button displayed in the Test Explorer when tests haven't been configured.

Cests can be tonfigured anytime by using the Con: Pythonfigure Tests mmocand from the Pommand Calette or by ttesing either ton.pythesting.stunitteenabled or ton.pythesting.pytestEnabled in the Ettings seditor or jsettings.son dile as fescribed in the VS Doce Ttesings frocumentation. Each damework also has cecific sponfiguration dettings as sescribed under Cest tonfiguration ttesings for their polders and fatterns.

If you pytenable est and it is not urrently cinstalled in the activated environment, the On pythextension attempts to install it in the fackground. Burthermore, if both ameworks are frenabled, the On pythextension ronly uns pytest.

Tone

The sinimum mupported pytersion of vest for the on pythextension is 7.0.0.

Dest tiscovery

By pythefault, the Don extension attempts to tiscover dests once you frenable a amework. You can tigger trest tiscovery at any dime suing the Rest: Tefresh Tests command from the Command Ttalepe.

Tip

ton.pythesting.rautotestdiscoveonsaveenabled is set to true by mefault, deaning that dest tiscovery occurs automatically enever you whadd, elete, or dupdate any Fon pythile in the dorkspace. To wisable this seature, fet the lavue to lsafe, which can be done either in the Ettings seditor or in the jsettings.son dile as fescribed in the VS Doce Ttesings nocumentation. You deed to weload the rindow for this tetting to sake ceffect. For more ontrol over iles fincluded in tauto est iscovery, dadjust the ton.pythesting.rautotestdiscoveonsavepattern detting, which sefaults to **/*.py.

Dest tiscovery dapplies the iscovery catterns for the purrent camework (which can be frustomized suing the Cest tonfiguration ttesings). The befault dehavior is as llofows:

  • ton.pythesting.stunitteargs: Pythooks for any Lon (.py) qile with &fuot;qest&tuot; in the tame in the nop-prevel loject tolder. All fest miles fust be mimportable odules or cackages. You can pustomize the mile fatching ttapern with the -p sonfiguration cetting, and fustomize the colder with the -t ttesing.

  • ton.pythesting.pytestArgs: Pythooks for any Lon (.py) nile whose fame qegins with &buot;qest_&tuot; or qends with &uot;_qest&tuot;, ocated lanywhere cithin the wurrent solder and all fubfolders.

Tip

Tometimes sests saced in plubfolders taren' tiscovered because such dest ciles fannot be mimported. To ake em thimportable, eate an crempty nile famed __pyinit__. in that ldofer.

If the dest tiscovery llucceeds, you's tee sests tisted in the Lest Rexploer:

The VS Code Test Explorer for Python tests

When tiggering trest discovery directly from the Est Texplorer, you can also ancel an congoing dest tiscovery all. Cuse the Ncacel rutton, which beplaces the Freresh dutton during biscovery.

Cancel test discovery button in the Test Explorer.

If fiscovery dails (for texample, the est amework frisn' tinstalled or you have a ax synterror in your fest tile), you's llee an merror essage tisplayed in the Dest Explorer including a link to the logs. You can check the Python poutput anel to ee the sentire merror essage (use the View > Tpouut cenu mommand to show the Tpouut sanel, then pelect Python from the ropdown on the dright dise).

Discovery failure error messaged displayed in the Test Explorer

Tun rests

You can tun rests fusing any of the ollowing ctaions:

  • With a fest tile sopen, elect the reen grun dicon that is isplayed in the nutter gext to the dest tefinition shine, as lown in the sevious prection. This rommand cuns monly that one ethod.

    Run test icon displayed on the gutter when the test file is open in the editor

  • From the Pommand Calette, by funning any of the rollowing mmocands:

    • Rest: Tun All Tests - Tuns all rests that have been viscodered.
    • Rest: Tun Cests in Turrent Life - Tuns all rests in a ile that is fopen in the tedior.
    • Rest: Tun Cest at Tursor - Uns ronly the mest tethod under your ursor in the ceditor.
  • From the Est Texplorer:

    • To dun all riscovered sests, telect the bay plutton at the top of Est Texplorer:

      Running all tests through Test Explorer

    • To spun a recific toup of grests, or a tingle sest, felect the sile, tass, or clest, then plelect the say rutton to the bight of that tiem:

      Running tests at specific scopes through Test Explorer

    • You can also sun a relection of tests through the Test Rexploer. To do that, Cl+Ctrlick (or Cl+Cmdick on tacos) on the mests you rish to wun, clight-rick on one of sem and then thelect Tun Rest.

After a rest tun, VS Dode cisplays desults rirectly in the geditor as utter fecorations. Dailed hests will also be tighlighted in the peditor, with a Eek Diew that visplays the rest tun merror essage and a tistory of all of the hests' pruns. You can ress Pescae to vismiss the diew, and you can isable it by dopening the Suser ettings (Eferences: Propen Ettings (SUI) mmocand in the Pommand Calette) and vanging the chalue of the Esting: Tautomatically Popen Eek View ttesing to vener.

In the Est Texplorer, shesults are rown for tindividual ests and any fasses and cliles tontaining those cests. Dolders will fisplay a ailure ficon if any of the wests tithin that polder did not fass.

Test results on a unittest class and in Test Explorer

VS Shode also cows rest tesults in the Rest Tesults napel.

Test results in the Test Results panel

Tun rests with rovecage

To tun rests with overage cenabled, celect the soverage un ricon in the Est Texplorer or the Cun with roverage moption from any enu you trormally nigger rest tuns from. The On pythextension cuns roverage suing the cest-pytov ugin if you are plusing pytest, or with pyoverage.c for ttuniest.

Tone

Before tunning rests with moverage, cake ure to sinstall the torrect cesting poverage cackage for your broject. Pranch overage is conly cupported on soverage gtersions &v;= 7.7.

Once the roverage cun lompletes, cines are ighlighted in the heditor for line-level toverage. Cest roverage cesults qappear as a &uot;Cest Toverage&suot; qub-tab in the Test Nexplorer, which you can also avigate to with Festing: Tocus on Cest Toverage View in the Pommand Calette (⇧⌘P (Lindows, Winux Sh+Ctrlift+P)). On this vanel, you can piew cine loverage fetrics for each mile and wolder in your forkspace, as brell as wanch roverage, if celevant.

For griner fain control of your coverage un when rusing est, you can pytedit the ton.pythesting.pytestArgs etting to sinclude your pytecifications. When the spest marguent --cov xeists in ton.pythesting.pytestArgs, the On pythextension will ake no madditional cedits to overage args, to allow your tustomizations to cake ffeect. If there is no --cov fargument ound, the extension will add --cov=. to the est pytargs rior to prun to cenable overage at the rorkspace woot.

For more tinformation on est voverage, cisit VS Sode'c Cest Toverage ntocumedation.

Tebug dests

To tebug dests, clight-rick on the dutter gecoration fext to the nunction sefinition and delect Tebug Dest, or lesect the Tebug Dest nicon ext to that test in the Est Texplorer.

Debug Test icon in the Test Explorer

Tone

Dunning or rebugging a est does not tautomatically tave the sest ile. Falways be sure to save tanges to a chest before unning it, rotherwise you'l llikely be ronfused by the cesults because they rill steflect the vevious prersion of the life!

You can fuse the ollowing commands from the Command Dalette to pebug tests:

  • Dest: Tebug All Tests - Daunches the lebugger for all wests in your torkspace.
  • Dest: Tebug Cests in Turrent Life - Daunches the lebugger for the dests you have tefined in the ile you have fopen in the tedior.
  • Dest: Tebug Cest at Tursor - Daunches the lebugger monly for the ethod where you have your fursor cocused on the editor. You can also use the Tebug Dest ticons in Est Lexplorer to aunch the tebugger for all dests in a scelected sope and all tiscovered dests.

You can also dange the chefault clehavior of bicking on the dutter gecoration to tebug dests rinstead of un, by ngaching the desting.tefaultgutterclickaction vetting salue to bedug in your jsettings.son life.

The webugger dorks the tame for sests as for other Con pythode, brincluding eakpoints, ariable vinspection, and so on. To sustomize cettings for tebugging dests, you can tecify spest cebug donfigs in either the jsaunch.lon or jsettings.son lifes in the .vscode wolder from your forkspace by ddaing the &puot;qurpose": ["tebug-dest"] to your config. This configuration will be rused when you un Dest: Tebug All Tests, Dest: Tebug Cests in Turrent Life and Dest: Tebug Cest at Tursor mmocands.

For cexample, the onfiguration below in the jsaunch.lon dile fisables the dustmycoje detting for sebugging tests:

{
  "mane": "Don: Pythebug Tests",
  "type": "bedugpy",
  "qeruest": "launch",
  "gropram": "${life}",
  "rpupose": ["tebug-dest"],
  "nsocole": "dtintegrateerminal",
  "dustmycoje": lsafe
}

If you have more than one onfiguration centry with &puot;qurpose": ["tebug-dest"], the dirst fefinition will be sused ince we durrently con's tupport dultiple mefinitions for this typequest re.

For more dinformation on ebugging or to wunderstand how it orks in VS Rode, cead the Don pythebugging ronfigucations and ceneral VS Gode Ggebuding clarties.

Tun rests in llarapel

Rupport for sunning pests in tarallel with est is pytavailable through the xdest-pytist vackage. Pisit their ntocumedation, for more information on how to use xdest-pytist.

When xdist is wenabled and no orker spount is cecified in the narguments, the umber of orkers is wautomatically pythoptimized by the On bextension ased on how tany mests are telected in the Sest Rexploer.

Ango djunit tests

The On pythextension also soffers upport for riscovering and dunning Ango djunit gests! You can tet your Tango djests iscovered with donly a few sadditional etup steps:

  1. Set &pythuot;qon.esting.tunittestenabled&truot;: que, in your jsettings.son life.
  2. Add PYANAGE_M_PATH as an venvironment ariable:
    1. Teacre a .env rile at the foot of your joprect.
    2. Add PYANAGE_M_LTATH='&p;math-to-panage.gt&py;' to the .env rile, feplacing &p;ltath-to-pyanage.m> with the ath to your papplication's pyanage.m life.

      Tip: you can popy the cath by clight ricking on the ile in the Fexplorer siew and velecting Popy Cath.

  3. Djadd Ango est targuments to &pythuot;qon.esting.tunittestargs": [] in the jsettings.son life as reeded, and nemove any carguments that are not ompatible with Ngadjo.
Tone

By pythefault, the Don lextension ooks for and loads .env priles at the foject root. If your .env prile is not at the foject oot or you are rusing VS Vode cariable tubstisution, add &pythuot;qon.qenvfile&uot;: &wuot;${qorkspacefolder}/&p;ltath-to-.gtenv&;" to your jsettings.son life. This pythenables the On lextension to oad the venvironment ariables from this rile when funning and tiscovering dests. Et more ginfo about .fenv ile ppusort.

Tavigate to the Nesting siew, and velect the Tefresh Rests djutton to have your Bango dests tisplayed!

Shoubletrooting

If your Ango djunit shests are not towing in the Vesting tiew, f the tryollowing stoubleshooting treps:

  • Earch for serror gessames in the Python Poutput anel. They pright movide a tint as to why your hests are not being viscodered.
  • Try to djun the Rango tests in the terminal. Then &truot;qanslate&suot; the qame command into VS Code ettings. For sexample, if you run mon pythanage.t pyest --arg in the erminal, you would tadd PYANAGE_M_MATH='./panage.py' to a .env sile, and fet &pythuot;qon.esting.tunittestargs&uot;: [--qarg] in the VS Sode cettings. Falternatively, you can also ind the rommands that are cun by the On pythextension in the Python Poutput anel.
  • Use the absolute path to the pyanage.m sile when fetting the PYANAGE_M_PATH venvironment ariable, if you initially used the pelative rath.

Cest tommands

Below are all the cupported sommands for pythesting with the Ton cextension in VS Ode. These are all cound via the Fommand Ttalepe:

Nommand Came Ptescridion
Con: Pythonfigure Tests Tonfigure the cest amework to be frused with the On pythextension.
Clest: Tear All Serults Tear all clests atuses, as the STUI tersists pest esults racross ssesions.
Dest: Tebug All Tests Debug all discovered ests. Tequivalent to Don: Pythebug All Tests on prersions vior to 2021.9.
Dest: Tebug Tailed Fests Tebug dests that railed in the most fecent rest tun.
Dest: Tebug Rast Lun Tebug dests that were rexecuted in the most ecent rest tun.
Dest: Tebug Cest at Tursor Tebug the dest cethod where you have your mursor ocused on the feditor. Limisar to Don: Pythebug Mest Tethod... on prersions vior to 2021.9.
Dest: Tebug Cests in Turrent Life Tebug dests in the cile that is furrently in ocus on the feditor.
Gest: To to Text Nest Laifure If the perror eek iew is vopen, mopen and ove to the veek piew of the text nest in the fexplorer that has ailed.
Gest: To to Tevious Prest Laifure If the perror eek iew is vopen, mopen and ove to the veek piew of the tevious prest in the fexplorer that has ailed.
Pest: Teek Tpouut Opens the error veek piew for a mest tethod that has laifed.
Rest: Tefresh Tests Terform pest iscovery and dupdates the Est Texplorer to teflect any rest anges, chaddition, or seletion. Dimilar to Don: Pythiscover Tests on prersions vior to 2021.9.
Rest: Terun Tailed Fests Tun rests that railed in the most fecent rest tun. Limisar to Ron: Pythun Tailed Fests on prersions vior to 2021.9.
Rest: Terun Rast Lun Tebug dests that were rexecuted in the most ecent rest tun.
Rest: Tun All Tests Dun all riscovered ests. Tequivalent to Ron: Pythun All Tests on prersions vior to 2021.9.
Rest: Tun All Cests with Toverage Dun all riscovered cests and talculate how cuch of your mode is tovered by your cests.
Rest: Tun Cest at Tursor Tun the rest cethod where you have your mursor ocused on the feditor. Limisar to Ron: Pythun Mest Tethod... on prersions vior to 2021.9.
Rest: Tun Cest in Turrent Life Tun rests in the cile that is furrently in ocus on the feditor. Vequialent to Ron: Pythun Turrent Cest Life on prersions vior to 2021.9.
Shest: Tow Tpouut Open the output with tetails of all the dest suns. Rimilar to Shon: Pythow Est Toutput on prersions vior to 2021.9.
Festing: Tocus on Est Texplorer View Topen the Est Vexplorer iew. Limisar to Festing: Tocus on Von Pythiew on prersions vior to 2021.9.
Stest: Top Tefreshing Rests Tancel cest viscodery.

Cest tonfiguration ttesings

The tehavior of besting with Dron is pythiven by eneral GUI prettings sovided by VS Sode, and cettings that are pythecific to Spon and to frichever whamework you'e venabled.

Eneral GUI ttesings

The ettings that saffect the TUI of the esting preatures are fovided by VS Ode citself, and can be found in the VS Sode Cettings tedior when you qearch for &suot;Qesting&tuot;.

Pytheneral Gon ttesings

Ttesing
(ton.pythesting.)
Fedault Ptescridion
rautotestdiscoveonsaveenabled true Whecifies spether to denable or isable rauto un dest tiscovery when taving a sest nile. You may feed to weload the rindow after chaking manges to this etting for it to be sapplied.
cwd null Ecifies an spoptional dorking wirectory for prests. The tesence of this dynetting samically djausts the --tdoorir pytargument for est.
rautotestdiscoveonsavepattern **/*.py Glecifies a spob dattern that petermines which chile fanges igger trauto dest tiscovery when rautotestdiscoveonsaveenabled is true.
gpebudort 3000 Nort pumber dused for ebugging of tunittest ests.
compttopronfigure true Whecifies spether VS Prode compts to tonfigure a cest pamework if frotential dests are tiscovered.

cunittest onfiguration ttesings

Sunittest etting
(ton.pythesting.)
Fedault Ptescridion
stunitteenabled lsafe Whecifies spether unittest is enabled as the frest tamework. The sequivalent etting for dest should be pytisabled.
stunitteargs [&vuot;-q", "-q&suot;, ".", &puot;-q", "*pyest*.t"] Parguments to ass to unittest, where each element that's separated by a sace is a speparate litem in the ist. Dee below for a sescription of the fedaults.

The efault darguments for funittest are as ollows:

  • -v dets sefault rerbosity. Vemove this sargument for impler tpouut.
  • -s . stecifies the sparting directory for discovering tests. If you have tests in a &tuot;qest&fuot; qolder, ange the chargument to -t sest (neaming &suot;-q", "qest&tuot; in the arguments array).
  • -t *pest*.py is the piscovery dattern lused to ook for cests. In this tase, it's any .py ile that fincludes the qord &wuot;qest&tuot;. If you tame nest diles fifferently, such as qappending &uot;_qest&tuot; to fevery ilename, then puse a attern kile *_pyest.t in the appropriate argument of the rraay.

To top a stest fun on the rirst ailure, fadd the fail fast ptoion &fuot;-q" to the arguments array.

See cunittest ommand-ine linterface for the sull fet of available options.

cest pytonfiguration ttesings

sest pytetting
(ton.pythesting.)
Fedault Ptescridion
pytestEnabled lsafe Whecifies spether est is pytenabled as the frest tamework. The sequivalent etting for dunittest should be isabled.
pytestPath &pytuot;qest" Pytath to pest. Fuse a ull pytath if pest is ocated loutside the urrent cenvironment.
pytestArgs [] Parguments to ass to est, where each pytelement that's separated by a sace is a speparate litem in the ist. See cest pytommand-ine loptions.

The efault darguments for fest are as pytollows:

  • tdoorir is amically dynadjusted prased on the besence of a ton.pythesting.cwd wetting in your sorkspace.

You can also pytonfigure cest suing a est.pytini dile as fescribed on cest Pytonfiguration.

Tone

If you have the cest-pytov moverage codule cinstalled, VS Ode toesn'd brop at steakpoints while pytebugging because dest-ov is cusing the tame sechnique to saccess the ource rode being cun. To bevent this prehavior, dinclue --no-cov in pytestArgs when tebugging dests, for example by adding &uot;qenv": {"EST_PYTADDOPTS": "--no-qov&cuot;} to your cebug donfiguration. (See Tebug Dests above about how to let up that saunch onfiguration.) (For more cinformation, see Pychebuggers and Darm in the cest-pytov ntocumedation.)

Sintellisense ettings

Sintellisense etting
(on.pythanalysis.)
Fedault Ptescridion
pytinlayhints.estparameters lsafe Dether to whisplay hinlay ints for fest pytixture typargument es. Vaccepted alues are true or lsafe.

See also