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

Question for write a Test with pytest in Plone #16

Open
1letter opened this issue Oct 11, 2024 · 6 comments
Open

Question for write a Test with pytest in Plone #16

1letter opened this issue Oct 11, 2024 · 6 comments

Comments

@1letter
Copy link

1letter commented Oct 11, 2024

@davisagli @ericof
Where is the best place for questions about pytest-plone? here or the community forum?

I play a little bit with the new pytest layer in an addon created with cookieplone

My goal: a functional test setup in a class with some sample content:

With the following setup all is fine. but the fixture is firing up in every method of my TestMyView class. This is not what i want. as far as I understand it, the reason is the scope of the fixture. in this case the "function scope".

# conftest.py

# global fixture to provide base structure for content
# can use in tests
@pytest.fixture
def contents_payload() -> list:
    """Payload to create two content items."""
    return [
        {
            "type": "Folder",
            "id": "test-folder",
            "title": "Test Folder",
            "description": "A Test Folder",
        },
        {
            "type": "Document",
            "id": "test-doc",
            "title": "Test Document",
            "description": "A Test Document",
        },
    ]

# fixture portal for functional test with test content
@pytest.fixture()
def portal(functional, content_payload):
    # the portal object for functional tests
    portal = functional["portal"]
    with api.env.adopt_roles(["Manager",]):
      for data in contents_payload:
        api.content.create(container=portal, **data)

    transaction.commit()
    return portal
# my Test Class
class TestMyView:
  def test_my_view1(self, portal):
    # do something in a functional test
    pass

  def test_my_view2(self, portal):
    # do something other stuff in a functional test
     pass

Now i changed the code to:

# fixture portal for functional test with test content
@pytest.fixture(scope="class")
def portal(functional_class, content_payload):
    # the portal object for functional tests
    portal = functional_class["portal"]
    with api.env.adopt_roles(["Manager",]):
      for data in contents_payload:
        api.content.create(container=portal, **data)

    transaction.commit()
    return portal

But this ends with an error:

self = <Layer 'collective.addon.testing.Collective.AddonLayer:FunctionalTesting'>, key = 'portal'

    def __getitem__(self, key):
        item = self.get(key, _marker)
        if item is _marker:
>           raise KeyError(key)
E           KeyError: 'portal'

.tox/test/lib/python3.12/site-packages/plone/testing/layer.py:28: KeyError

Where is my mistake? I thought, i can switch easily the scope, and no more changes are needed.

I'm new in the pytest world, please bear with me.

@stevepiercy
Copy link
Member

Where is the best place for questions about pytest-plone? here or the community forum?

@1letter both! Thank you for bringing this up. I assume many developers are struggling through the same challenges that you face. A question in the Community Forum exposes this to a broader audience than just this repo, and increases the probability of getting answers that we can put into documentation. Cross-link between that post and this issue so we can track it.

@ericof
Copy link
Member

ericof commented Oct 11, 2024

@stevepiercy, either way this is a very good question. I was planning to address this during my talk at the conference, but @sneridagh pushed me to get better documentation for pytest-plone sooner than that.
My question here would be: Should I add it here to this repo, to the testing training or elsewhere?

@stevepiercy
Copy link
Member

@ericof I think the Plone 6 documentation is the most important place to put it. That gets the most exposure and has the best SEO, and it is maintained perpetually instead of once per PloneConf.

FYI, we recently revised the Install entry point to the documentation. One of the options is now Create a project with Volto (development or pre-release).

We need similar documentation, "Create a project with Classic UI (development or pre-release)". It should target developers or integrators, especially those who currently use buildout to make the transition. There may be some snippets that you can reuse from that Volto guide. In fact you could probably copy that file and remove any Volto-specific things such as Node.js prerequisites, and your work might be already halfway done for you.

Install should be the foundation. I expect that the usual development tasks—create and manage add-ons, tests, code quality—will be separate pages or topics.

Imagine with that documentation in place, you could refer to that in your talk and training.

As always, don't worry about proper English or MyST spelling, grammar, and syntax. The most important thing is to document the steps that a developer would follow to start from nothing and end with a working Classic UI environment to develop and test an add-on or project. I'm always in Discord in the Documentation channel, if you have questions or need a helping hand.

@davisagli
Copy link
Member

@stevepiercy I'm a little confused by your comment. This issue is about how to document writing tests for Plone backend packages using pytest, but you're talking about documentation for how to create a Classic UI project.

The choice of whether to test your Python packages using unittest and the Zope testrunner or using pytest is orthogonal to the choice of whether to use the Classic UI or Volto as your frontend. You can use either unittest or pytest to write tests for any Python package, including both for Classic UI projects and for the backend portion of a Volto project or add-on.

I think we probably want a conceptual guide which gives a very high level overview of different types of testing, and then a Testing subsection of the new Development Guide you started in plone/documentation#1731, structured something like this:

@stevepiercy
Copy link
Member

@davisagli I assumed that the choice of Plone installation method would determine which test runner to use, as Cookieplone currently uses pytest for the default.

In any case, I think your outline is excellent, with possibly the exception of the Robot Framework. Isn't that being replaced with Playwright in Plone 6.1?

@davisagli
Copy link
Member

I assumed that the choice of Plone installation method would determine which test runner to use, as Cookieplone currently uses pytest for the default.

There's no fundamental reason that has to be the case. The cookiecutter-plone-starter template offered a choice between unittest and pytest, and the new cookieplone template could as well with a little work.

In any case, I think your outline is excellent, with possibly the exception of the Robot Framework. Isn't that being replaced with Playwright in Plone 6.1?

Not exactly. There are efforts to rewrite Plone core's Robot Framework tests to use a new set of commands that is implemented with Playwright instead of selenium, but it's still Robot Framework, and it's still possible to use the old commands. plone/Products.CMFPlone#3813

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants