You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio 2017中调用Python的doctest?

Running doctests in Visual Studio 2017 for Python Projects

Hey there! I’ve been in your shoes before—getting set up with Python in VS2017 and trying to get doctests running can feel a bit tricky at first. Let’s walk through reliable ways to get your tests up and running, plus troubleshoot the issues you’ve hit.

Method 1: Use Visual Studio's Test Explorer (Integrated Approach)

This is the most seamless way to run doctests right within VS:

  • First, double-check your doctest syntax is correct (example below to confirm):
    def multiply(a, b):
        """
        Multiplies two numbers.
        >>> multiply(4, 5)
        20
        >>> multiply(0, 100)
        0
        """
        return a * b
    
  • Open the Test Explorer: Go to Test > Windows > Test Explorer from the top menu bar.
  • Configure test discovery: Right-click your Python project in Solution Explorer, select Properties, then navigate to the Test tab. Make sure the Test framework dropdown is set to doctest, then click Apply.
  • Run your tests: Back in Test Explorer, click the Run All button. You’ll see each doctest case listed, with pass/fail statuses right in the window.

Method 2: Run doctests in the Python Interactive Window

Your earlier attempt might have failed due to incorrect working directory or import issues. Try these steps:

  • Open the Python Interactive Window via View > Other Windows > Python Interactive.
  • First, switch to the directory where your script is located:
    import os
    # Replace with your actual script folder path
    os.chdir("C:\\YourPythonProjectFolder")
    
  • Now run the tests using either of these commands:
    # Option 1: Import the module and test it
    import myfile  # Replace "myfile" with your script name (no .py)
    import doctest
    doctest.testmod(myfile)
    
    # Option 2: Test directly via the file path
    doctest.testfile("myfile.py")
    

You’ll see output in the window showing how many tests passed/failed.

Method 3: Use VS's Terminal (Not Package Manager Console)

The Package Manager Console (PM>) is for NuGet commands—you want a regular terminal instead:

  • Open the terminal in VS: Go to View > Terminal.
  • Navigate to your script's directory:
    cd C:\YourPythonProjectFolder
    
  • Run the doctest command:
    python -m doctest myfile.py
    

If you get a "python not found" error, use the full path to your Python executable (from your VS Python environment):

C:\Path\To\Your\Python\python.exe -m doctest myfile.py

Common Troubleshooting Tips

  • Path issues: Always confirm you're working in the directory where your script lives—Python can't find files outside the current working directory unless you use absolute paths.
  • Doctest syntax errors: Even small formatting mistakes break tests. Make sure >>> has a space after it, and the expected result matches exactly what your function returns (including whitespace, capitalization, and data type).
  • Wrong Python environment: VS2017 might have multiple Python environments configured. Check which one you're using via View > Other Windows > Python Environments—switch to the one you used to write your script if needed.

内容的提问来源于stack exchange,提问作者Lorettac242

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:35:50