camptocamp / pytest-odoo

pytest plugin to run Odoo tests
GNU Affero General Public License v3.0
67 stars 41 forks source link

Can't Test Controllers #44

Closed eselimsen closed 2 years ago

eselimsen commented 2 years ago

Hi First of all, thank you for maintaining this module, it really fills the gap on testing.

I've been using module for a while, maybe more than a year, but still couldn't find a "official" solution for controller testing. Looks like we're telling Odoo server to exit immediately, then move on to other steps. Since there's no Odoo server to make requests to, I can't test my controllers, I have to spin up another server process and make requests to that server. I made some wrappers and helpers to auto-clean resources created on server process, but it's still far away from perfect, let alone I can't access to logs etc.

I assume you're aware of this, but is there a way to solve this, am I missing something?

yvaucher commented 2 years ago

Hi @eselimsen

You might have missed the HttpCase class for tests.

from odoo.tests import HttpCase

class TestController(HttpCase):
    def test_something(self):
        # ...
        result = self.url_open(some_url)
        # ...

https://github.com/odoo/odoo/blob/15.0/addons/test_website/tests/test_controller_args.py

eselimsen commented 2 years ago

Thanks, I checked that, but I want to test server-side actions, not just responses. It causes leftover records, and I can't check logs (pretty important for me).

However, today I started Mocking request object, like website does here, I'll see how well it does.

yvaucher commented 2 years ago

Have you tried using pytest --odoo-log-level=...?

simahawk commented 2 years ago

@eselimsen HttpCase is not supported yet here. What I do usually when I want to ensure a route is registered and called properly is to split the tests (sort of unit + integration tests).

Here, normal tests that require a request (mocked with this).

And here tests for the controllers. Note the decorator @unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "EndpointHttpCase skipped") which allows me to run the whole test suite via pytest w/out breaking, using SKIP_HTTP_CASE=1 pytest --odoo-database foo [...].

eselimsen commented 2 years ago

Looks like mocking request is the best for me now, also skipIf looks interesting too, thanks. @simahawk