1

I have a python test file using the unittest framework and test discovery fails because of a TypeError. Is there a way in Visual Studio Code to show the line number that causes this TypeError? It is really cumbersome to comment out tests to find the culprit

IDE

Nico Müller
  • 1,784
  • 1
  • 17
  • 37
  • 1
    Probably not helpful, and you've probably already found it, but does any of this help: https://stackoverflow.com/questions/55837922/vscode-pytest-test-discovery-fails (I'm not a fan of IDE´s so I have no clue what might help, but thought i'd try to help in any way I can until some real pro's step in) – Torxed May 30 '20 at 20:50
  • Thanks for your help. Unfortunately, this worked flawlessly before and there was no new dependency added. I just added a few more tests. I just wonder if there is any way to get more helpful information from the test output where this TypeError is coming from. – Nico Müller May 30 '20 at 20:53
  • Not sure, I don't think it's an IDE issue, it's probably has to do with the test library not fully printing the complete stack trace. Buf it you want, try running the code in a terminal instead of the IDE, see if that gives more debug data (haaardly doubt it tho). Is there a verbose mode in the test library? – Torxed May 30 '20 at 20:55
  • Does pytest discovery work from the command-line when you run it manually (look in the Output panel and the Python channels to see the command the extension is running on your behalf)? – Brett Cannon Jun 01 '20 at 23:43
  • @BrettCannon yes executing the test script from IDE or via `pytest test.py` works but I do not see any discovered tests in visual studio code – Nico Müller Jun 02 '20 at 17:15
  • 1
    Please open an issue at https://github.com/microsoft/vscode-python with a reproducible sample so we can try to help you diagnose the problem. – Brett Cannon Jun 02 '20 at 21:59
  • @BrettCannon I saw that someone already opened one: https://github.com/microsoft/vscode-python/issues/12043 – Nico Müller Jun 03 '20 at 12:42

1 Answers1

1

1.pytest works charmly with VS Code. More info is here.

pytest --tb=line

modifying-python-traceback-printing

2.Or use pytest-instafail

pip install pytest-instafail

Sample code:

def print_failure(self, report):
    if self.config.option.tbstyle != "no":
        if self.config.option.tbstyle == "line":
            line = self._getcrashline(report)
            self.write_line(line)
        else:
            msg = self._getfailureheadline(report)
            # "when" was unset before pytest 4.2 for collection errors.
            when = getattr(report, "when", "collect")
            if when == "collect":
                msg = "ERROR collecting " + msg
            elif when == "setup":
                msg = "ERROR at setup of " + msg
            elif when == "teardown":
                msg = "ERROR at teardown of " + msg
            self.write_sep("_", msg)
            if not self.config.getvalue("usepdb"):
                self._outrep_summary(report)

pytest_instafail

Mahsa Hassankashi
  • 2,086
  • 1
  • 15
  • 25