Skip to content
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

Issues with PytestDeprecationWarning on direct construction of pytest.File/Item #7696

Closed
andy-maier opened this issue Aug 27, 2020 · 2 comments
Labels
topic: collection related to the collection phase type: docs documentation improvement, missing or needing clarification type: question general question, might be closed after 2 weeks of inactivity

Comments

@andy-maier
Copy link
Contributor

Hi. We have implemented a pytest collector quite some time ago that gets testcases from YAML files. I have noticed the following deprecation warnings recently:

tests/functiontest/conftest.py:221
  /Users/maiera/PycharmProjects/pywbem/pywbem/tests/functiontest/conftest.py:221:
  PytestDeprecationWarning: direct construction of YamlFile has been deprecated, please use YamlFile.from_parent
    return YamlFile(path, parent)

tests/functiontest/conftest.py:249
  /Users/maiera/PycharmProjects/pywbem/pywbem/tests/functiontest/conftest.py:249:
  PytestDeprecationWarning: direct construction of YamlItem has been deprecated, please use YamlItem.from_parent
    yield YamlItem(tc_name, self, testcase, filepath)

The source code for the collection is here: https://github.com/pywbem/pywbem/blob/master/tests/functiontest/conftest.py#L225

I have the following issues with that:

  1. The pytest.File class is missing completely from the pytest API Reference docs.

  2. The pytest.Item class does not list a from_parent() method in the pytest API Reference docs for Item.

  3. For our YamlFile, I was able to map the arguments from the ctor to the from_parent() method, but for our YamlItem it turns out we are passing the YAML file name as an additional argument to the YamlItem ctor (for better error reporting), and I did not find a way to specify that via the from_parent() method. Is there some way for the YamlItem object to get at the file name of the YAML file? If not, could you possibly allow for additional kwargs that are passed on?

Versions:

Python 3.8.5 on MacOs
$ pytest --version
This is pytest version 5.4.3, imported from /Users/maiera/virtualenvs/pywbem38/lib/python3.8/site-packages/pytest/__init__.py
setuptools registered plugins:
  requests-mock-1.8.0 at /Users/maiera/virtualenvs/pywbem38/lib/python3.8/site-packages/requests_mock/contrib/_pytest_plugin.py
  yagot-0.5.0 at /Users/maiera/virtualenvs/pywbem38/lib/python3.8/site-packages/yagot_pytest/plugin.py
  pytest-cov-2.10.1 at /Users/maiera/virtualenvs/pywbem38/lib/python3.8/site-packages/pytest_cov/plugin.py

We have pinned pytest to <6.0.0 due to issue #7591 which was fixed meanwhile (thanks for that!!) but not yet released. After realizing that, I retested with pytest 6.0.1 and the same warnings were issued.

@nicoddemus
Copy link
Member

Hi @andy-maier,

We have implemented a pytest collector quite some time ago that gets testcases from YAML files. I have noticed the following deprecation warnings recently:

Indeed this is part of an ongoing internal refactoring that unfortunately needed to be spilled out to user plugins, sorry about that.

The pytest.File class is missing completely from the pytest API Reference docs.

Indeed that was missing, opened a PR with a fix: #7697.

The pytest.Item class does not list a from_parent() method in the pytest API Reference docs for Item.

It is listed in its parent class, Node.

For our YamlFile, I was able to map the arguments from the ctor to the from_parent() method, but for our YamlItem it turns out we are passing the YAML file name as an additional argument to the YamlItem ctor (for better error reporting), and I did not find a way to specify that via the from_parent() method. Is there some way for the YamlItem object to get at the file name of the YAML file? If not, could you possibly allow for additional kwargs that are passed on?

That should already be possible, from_parent accepts arbirtrary keyword arguments, so this should work:

yield YamlItem.from_parent(name=tc_name, parent=self, testcase=testcase, filepath=filepath)

Hope that helps. Please let us know if there are still questions. 👍

@Zac-HD Zac-HD added topic: collection related to the collection phase type: docs documentation improvement, missing or needing clarification type: question general question, might be closed after 2 weeks of inactivity labels Aug 28, 2020
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Aug 28, 2020
@andy-maier
Copy link
Contributor Author

andy-maier commented Sep 17, 2020

Hi @nicoddemus . Thanks for these explanations and for the PR! I'll try it.

Update: Tried it and it works fine. Since we are still supporting py27/34/35, we are now using from_file() when that method is on the object and the direct creation otherwise.
Issue can be closed.

Thanks again!! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase type: docs documentation improvement, missing or needing clarification type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants