Skip to content

Reported location for assertion error is off with coverage #5754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
blueyed opened this issue Aug 17, 2019 · 0 comments
Closed

Reported location for assertion error is off with coverage #5754

blueyed opened this issue Aug 17, 2019 · 0 comments
Labels
topic: rewrite related to the assertion rewrite mechanism type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@blueyed
Copy link
Contributor

blueyed commented Aug 17, 2019

Locations for assertion errors are off when using pytest with coverage (pytest-cov):

    def test_config():
        assert False
>       assert False
E       assert False

Bisected to 39ba996.

More information (original issue):

In https://travis-ci.com/pdbpp/fancycompleter/jobs/226053243#L153 it reports this error:

================================== FAILURES ===================================
_________________________________ test_config _________________________________
tmphome = local('C:\\Users\\travis\\AppData\\Local\\Temp\\pytest-of-unknown\\pytest-0\\test_config0')
capsys = <_pytest.capture.CaptureFixture object at 0x0000026CE8AD3508>
    def test_config(tmphome, capsys):
        class DefaultCfg:
            default = 42
    
        class MyCfg(ConfigurableClass):
            DefaultConfig = DefaultCfg
            config_filename = ".mycfg"
    
        cfg = MyCfg()
    
        # Calls passed in Config.
        assert cfg.get_config(Config=lambda: "42") == "42"
    
        # Uses DefaultConfig instance.
        assert isinstance(cfg.get_config(None), DefaultCfg)
    
        cfgfile = tmphome.join(MyCfg.config_filename)
    
        # Handles empty config file (missing "Config").
        cfgfile.ensure()
        assert isinstance(cfg.get_config(None), DefaultCfg)
        out, err = capsys.readouterr()
        assert out == ""
        assert err == ""
    
        # Exception when instantiating Config.
        p = tmphome.ensure(MyCfg.config_filename)
        print(p, file=capsys._capture.out._old)
        cfgfile.write("def Config(): raise Exception('my_exc')")
        assert isinstance(cfg.get_config(None), DefaultCfg)
        out, err = capsys.readouterr()
        assert out == ""
        assert err == (
            "** error when setting up Config from ~/.mycfg: my_exc (%s:1) **\n" % p
        )
        print("passed first", file=capsys._capture.out._old)
    
        # Error during execfile.
        config_filename = tmphome.ensure(MyCfg.config_filename)
        print("config_filename:", config_filename, file=capsys._capture.out._old)
        cfgfile.write("raise Exception('my_execfile_exc')")
        assert isinstance(cfg.get_config(None), DefaultCfg)
        out, err = capsys.readouterr()
        assert out == ""
>       assert err == "** error when importing ~/.mycfg: my_execfile_exc **\n"
E       AssertionError: assert '** error whe...mycfg:1) **\n' == '** error whe...mycfg:1) **\n'
E         Skipping 119 identical leading characters in diff, use -v to show
E         - st_config0/.mycfg:1) **
E         ?           ^
E         + st_config0\.mycfg:1) **
E         ?           ^
C:\Users\travis\build\pdbpp\fancycompleter\testing\test_configurableclass.py:48: AssertionError
---------------------------- Captured stdout call -----------------------------
C:\Users\travis\AppData\Local\Temp\pytest-of-unknown\pytest-0\test_config0\.mycfg

Full log: https://travis-ci.com/pdbpp/fancycompleter/jobs/226053243#L153

Note that it reports the error with the wrong assert statement - which can be seen also by "passed first" not being included in "Captured stdout call".

With --assert=plain it is correct:

_________________________________ test_config _________________________________
tmphome = local('C:\\Users\\travis\\AppData\\Local\\Temp\\pytest-of-unknown\\pytest-0\\test_config0')
capsys = <_pytest.capture.CaptureFixture object at 0x00000170297C1CC8>
    def test_config(tmphome, capsys):
        class DefaultCfg:
            default = 42
    
        class MyCfg(ConfigurableClass):
            DefaultConfig = DefaultCfg
            config_filename = ".mycfg"
    
        cfg = MyCfg()
    
        # Calls passed in Config.
        assert cfg.get_config(Config=lambda: "42") == "42"
    
        # Uses DefaultConfig instance.
        assert isinstance(cfg.get_config(None), DefaultCfg)
    
        cfgfile = tmphome.join(MyCfg.config_filename)
    
        # Handles empty config file (missing "Config").
        cfgfile.ensure()
        assert isinstance(cfg.get_config(None), DefaultCfg)
        out, err = capsys.readouterr()
        assert out == ""
        assert err == ""
    
        # Exception when instantiating Config.
        p = tmphome.ensure(MyCfg.config_filename)
        print(p, file=capsys._capture.out._old)
        cfgfile.write("def Config(): raise Exception('my_exc')")
        assert isinstance(cfg.get_config(None), DefaultCfg)
        out, err = capsys.readouterr()
        assert out == ""
        assert err == (
>           "** error when setting up Config from ~/.mycfg: my_exc (%s:1) **\n" % p
        )
E       AssertionError
C:\Users\travis\build\pdbpp\fancycompleter\testing\test_configurableclass.py:37: AssertionError
---------------------------- Captured stdout call -----------------------------
C:\Users\travis\AppData\Local\Temp\pytest-of-unknown\pytest-0\test_config0\.mycfg

platform win32 -- Python 3.7.4, pytest-5.1.0, py-1.8.0, pluggy-0.12.0

@blueyed blueyed added type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously topic: rewrite related to the assertion rewrite mechanism labels Aug 17, 2019
blueyed added a commit to blueyed/pytest that referenced this issue Aug 17, 2019
blueyed added a commit to blueyed/pytest that referenced this issue Aug 17, 2019
blueyed added a commit that referenced this issue Aug 17, 2019
Fix wrong location of assertion error with Coverage.py .

Reverts using-constant part from 39ba996.

Fixes #5754.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: rewrite related to the assertion rewrite mechanism type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

No branches or pull requests

1 participant