PTVS icon indicating copy to clipboard operation
PTVS copied to clipboard

Question: How to run unit tests from a TFS Build Definition?

Open ThomasRapidis opened this issue 10 years ago • 7 comments

In Visual Studio, we can easily create TFS Build Definitions and configure these to execute C# unit tests as part of the build, so that the build inly succeeds if the tests pass.

Is there a way to do this for python unit tests?

ThomasRapidis avatar Sep 24 '16 13:09 ThomasRapidis

I would suggest following the unittest documentation and running them directly (i.e. with a command line step). There are some nuget packages for Python if you don't already have it installed on your build machine.

The only tricky part is getting nice integration into the test results display. There may be plugins to produce compatible results files, but I don't know of any off the top of my head.

zooba avatar Sep 24 '16 14:09 zooba

Hi guys,

Did you find a way to run your tests as I'm having a similar problem.

one resource I found here suggests ptvs will work with builds however I cant seem to get it to work. it states that the plugin will be a .vsix file though I can only find a download for a .msi

Is there somewhere I can retrieve a PTVS.visx file from to see if this will work?

liamharries avatar Nov 02 '16 10:11 liamharries

No, that won't work.

Try installing Python and pytest on your build machine and running that directly. Visual Studio doesn't actually add any functionality for running tests, we just run the regular tools for you, so if you aren't actively using VS then it's best to run the tools directly.

zooba avatar Dec 27 '16 18:12 zooba

@zooba , Thanks for your response to @liamharries 's question. I guess I don't understand the full response and what you mean by "... we just run the regular tools for you...". I've been trying for a few days to find a way to integrate python tests within a TFS 2013 (XAML-based) build system so I can have the build system discover my tests which use unittest, execute the tests, and commit whatever python code I'm checking in. So far I haven't been able to get my tests to be discovered by the build system like they can be discovered in Visual Studio with PTVS. I went further than @liamharries and un-packed the contents of the 2013 PTVS .msi file and committed that in the build using the MSDN instructions. I take it from your response, that the MSDN instructions are incorrect and PTVS won't help a TFS 2013 build system discover and run python tests the way Visual Studio does. Is that correct?

Also, you mention an alternative using pytest somehow on the build. Can you elaborate on how to setup such a scheme so that I can reach the goal of having a TFS 2013 build system discover and run python unit tests in Continuous Integration (CI) builds?

By the way, if there is functionality that PTVS provides in TFS 2017 to solve my aforementioned problem of discovering/running python tests, I'm willing to upgrade to TFS 2017 and abandon TFS 2013. Also, something else to consider is that my build definition is specifically looking for a .dll file in the "Test assembly file specification" field (see attached image) and since python doesn't use .dlls, I tried to point to the relative path of where my test files live. That may be why TFS isn't discovering/running python tests with PTVS. I'm not sure.

python_config1

RudyD avatar Mar 23 '17 16:03 RudyD

@ everyone: Good news. Please see the comments here: https://blogs.msdn.microsoft.com/pythonengineering/2017/03/07/python-support-in-vs2017/#comment-1385.

In short: Use pytest to run the tests on the server and have it output test results in the JUnit format. TFS Build has a Publish Test Results task, which can pick those up. Not in XAML builds, though.

ThomasRapidis avatar Mar 23 '17 16:03 ThomasRapidis

Yeah, XAML build definitions are tough to extend. If you're stuck using them, probably the best thing to do is to write a C# DLL that calls each Python test (and I agree, that's not a very good thing to do).

TFS 2015 build (and later) have more flexible steps, so you can use the Command line task to run pytest, and then the "Publish Test Results" as Thomas mentioned.

As your screenshot shows, the XAML build definition is essentially locked to using the Visual Studio Test Runner. For Python, we extend that with a very thin layer that converts the test into a call into the unittest module and translates the output into VS format. It is very much only meant for using interactively - Python tests are much better run using the actual Python tools such as pytest or unittest.

zooba avatar Mar 23 '17 18:03 zooba

Thanks @ThomasRapidis and @zooba . Really appreciate the quick response. I'll go ahead and attempt to download and install TFS 2015 (or I guess now, at the time of this writing, it's TFS 2017) and configure my python tests to use pytest to publish test results.

RudyD avatar Mar 24 '17 04:03 RudyD