question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pytest is trying to access sensitive data/private path when running 'pytest --fixtures' on macOS

See original GitHub issue
  • a detailed description of the bug or suggestion

When trying to list the available built in fixture in pytest by simply running ‘pytest --fixtures’ an error is thrown as I did not give the permission to access my sensitive data.

Why pytest is trying to access my addressBook?

  • output of pip list from the virtual environment you are using

=> pip list Package Version


atomicwrites 1.3.0
attrs 19.1.0 importlib-metadata 0.19
more-itertools 7.2.0
packaging 19.1
pip 19.2.3 pluggy 0.12.0 py 1.8.0
pyparsing 2.4.2
pytest 5.1.2
setuptools 41.2.0 six 1.12.0 wcwidth 0.1.7
wheel 0.33.6 zipp 0.6.0

  • pytest and operating system versions

=> uname -a Darwin xxxx-MacBook-Pro.local 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64

=> pytest --version This is pytest version 5.1.2, imported from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytest.py

  • minimal example if possible

=> pytest --fixtures =========================================================================================== test session starts ============================================================================================ platform darwin – Python 3.7.4, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: /Users/xxxx collected 0 items / 1 errors
cache Return a cache object that can persist state between testing sessions.

cache.get(key, default)
cache.set(key, value)

Keys must be a ``/`` separated value, where the first part is usually the
name of your plugin or application to avoid clashes with other cache users.

Values can be any object handled by the json stdlib module.

capsys Enable text capturing of writes to sys.stdout and sys.stderr.

The captured output is made available via ``capsys.readouterr()`` method
calls, which return a ``(out, err)`` namedtuple.
``out`` and ``err`` will be ``text`` objects.

capsysbinary Enable bytes capturing of writes to sys.stdout and sys.stderr.

The captured output is made available via ``capsysbinary.readouterr()``
method calls, which return a ``(out, err)`` namedtuple.
``out`` and ``err`` will be ``bytes`` objects.

capfd Enable text capturing of writes to file descriptors 1 and 2.

The captured output is made available via ``capfd.readouterr()`` method
calls, which return a ``(out, err)`` namedtuple.
``out`` and ``err`` will be ``text`` objects.

capfdbinary Enable bytes capturing of writes to file descriptors 1 and 2.

The captured output is made available via ``capfd.readouterr()`` method
calls, which return a ``(out, err)`` namedtuple.
``out`` and ``err`` will be ``byte`` objects.

doctest_namespace [session scope] Fixture that returns a :py:class:dict that will be injected into the namespace of doctests.

pytestconfig [session scope] Session-scoped fixture that returns the :class:_pytest.config.Config object.

Example::

    def test_foo(pytestconfig):
        if pytestconfig.getoption("verbose") > 0:
            ...

record_property Add an extra properties the calling test. User properties become part of the test report and are available to the configured reporters, like JUnit XML. The fixture is callable with (name, value), with value being automatically xml-encoded.

Example::

    def test_function(record_property):
        record_property("example_key", 1)

record_xml_attribute Add extra xml attributes to the tag for the calling test. The fixture is callable with (name, value), with value being automatically xml-encoded

record_testsuite_property [session scope] Records a new <property> tag as child of the root <testsuite>. This is suitable to writing global information regarding the entire test suite, and is compatible with xunit2 JUnit family.

This is a ``session``-scoped fixture which is called with ``(name, value)``. Example:

.. code-block:: python

    def test_foo(record_testsuite_property):
        record_testsuite_property("ARCH", "PPC")
        record_testsuite_property("STORAGE_TYPE", "CEPH")

``name`` must be a string, ``value`` will be converted to a string and properly xml-escaped.

caplog Access and control log capturing.

Captured logs are available through the following properties/methods::

* caplog.text            -> string containing formatted log output
* caplog.records         -> list of logging.LogRecord instances
* caplog.record_tuples   -> list of (logger_name, level, message) tuples
* caplog.clear()         -> clear captured records and formatted log output string

monkeypatch The returned monkeypatch fixture provides these helper methods to modify objects, dictionaries or os.environ::

    monkeypatch.setattr(obj, name, value, raising=True)
    monkeypatch.delattr(obj, name, raising=True)
    monkeypatch.setitem(mapping, name, value)
    monkeypatch.delitem(obj, name, raising=True)
    monkeypatch.setenv(name, value, prepend=False)
    monkeypatch.delenv(name, raising=True)
    monkeypatch.syspath_prepend(path)
    monkeypatch.chdir(path)

All modifications will be undone after the requesting
test function or fixture has finished. The ``raising``
parameter determines if a KeyError or AttributeError
will be raised if the set/deletion operation has no target.

recwarn Return a :class:WarningsRecorder instance that records all warnings emitted by test functions.

See http://docs.python.org/library/warnings.html for information
on warning categories.

tmpdir_factory [session scope] Return a :class:_pytest.tmpdir.TempdirFactory instance for the test session.

tmp_path_factory [session scope] Return a :class:_pytest.tmpdir.TempPathFactory instance for the test session.

tmpdir Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a py.path.local_ path object.

.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html

tmp_path Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a :class:pathlib.Path object.

.. note::

    in python < 3.6 this is a pathlib2.Path

================================================================================================== ERRORS ================================================================================================== ______________________________________________________________________________________ ERROR collecting test session _______________________________________________________________________________________ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_error.py:66: in checked_call return func(*args, **kwargs) E PermissionError: [Errno 1] Operation not permitted: ‘/Users/xxxx/Library/Application Support/AddressBook’

During handling of the above exception, another exception occurred: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_path/common.py:377: in visit for x in Visitor(fil, rec, ignore, bf, sort).gen(self): /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_path/common.py:429: in gen for p in self.gen(subdir): /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_path/common.py:429: in gen for p in self.gen(subdir): /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_path/common.py:429: in gen for p in self.gen(subdir): /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_path/common.py:414: in gen entries = path.listdir() /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_path/local.py:392: in listdir names = py.error.checked_call(os.listdir, self.strpath) /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/py/_error.py:86: in checked_call raise cls(“%s%r” % (func.name, args)) E py.error.EPERM: [Operation not permitted]: listdir(‘/Users/xxxx/Library/Application Support/AddressBook’,) ============================================================================================= 1 error in 0.14s =============================================================================================

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
asottilecommented, Sep 4, 2019

I don’t agree with you – garbage in garbage out essentially. There’s no reason to ever run pytest from / or from ~.

Here’s a much simpler reproduction:

mkdir y
chmod 000 y
pytest
$ pytest 
============================= test session starts ==============================
platform linux -- Python 3.6.8, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /tmp/x
collected 0 items / 1 errors                                                   

==================================== ERRORS ====================================
________________________ ERROR collecting test session _________________________
venv/lib/python3.6/site-packages/py/_error.py:66: in checked_call
    return func(*args, **kwargs)
E   PermissionError: [Errno 13] Permission denied: '/tmp/x/y'

During handling of the above exception, another exception occurred:
venv/lib/python3.6/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
venv/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
venv/lib/python3.6/site-packages/py/_path/common.py:414: in gen
    entries = path.listdir()
venv/lib/python3.6/site-packages/py/_path/local.py:392: in listdir
    names = py.error.checked_call(os.listdir, self.strpath)
venv/lib/python3.6/site-packages/py/_error.py:86: in checked_call
    raise cls("%s%r" % (func.__name__, args))
E   py.error.EACCES: [Permission denied]: listdir('/tmp/x/y',)
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.12s ===============================

Should this be an error? absolutely

0reactions
amdshrifcommented, Sep 5, 2019

@asottile @nicoddemus Thanks for clarifying this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest fixtures: explicit, modular, scalable
fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component ...
Read more >
How do you run repeated routines/functions inside a test in ...
Fixtures are pytest's way of managing and sharing sets of resources and the instructions to arrange them in some way. This is why...
Read more >
User Guide — pytest-selenium latest documentation
The pytest-selenium plugin provides a function scoped selenium fixture for your tests. This means that any test with selenium as an argument will...
Read more >
All You Need To Know To Start Using Fixtures In Your Pytest ...
The @pytest.fixture decorator provides an easy yet powerful way to setup and teardown resources. You can then pass these defined fixture objects ...
Read more >
Pytest in the Networking World - Part 2 - NTC Blog
This post will go over various aspects of parameterization. It is a very powerful tool that allows us to run tests in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found