Skip to content

Support for SimpleTestCase.assertTemplateUsed etc #97

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 Jun 5, 2014 · 7 comments
Closed

Support for SimpleTestCase.assertTemplateUsed etc #97

blueyed opened this issue Jun 5, 2014 · 7 comments

Comments

@blueyed
Copy link
Contributor

blueyed commented Jun 5, 2014

I wonder if there is currently a way to use e.g. methods like assertTemplateUsed from Django's SimpleTestCase with pytest-django directly.

I know that I could use a standard SimpleTestCase, but maybe I am just missing something.
What would be the approach to get things like this into pytest-django, if this would be desired?

@rafales
Copy link
Contributor

rafales commented Jun 5, 2014

There's an easy way to achieve that:

def pytest_namespace():
    """Make unittest assert methods available.
    This is useful for things such floating point checks with assertAlmostEqual.
    """

    import unittest

    class Dummy(unittest.TestCase):
        def dummy(self):
            pass
    obj = Dummy('dummy')

    names = {name: member
             for name, member in inspect.getmembers(obj)
             if name.startswith('assert') and '_' not in name}

    return names

Just replace unittest.TestCase with SimpleTestCase.

This makes all methods from TestCase available on the pytest namespace (you can do pytest.assertAlmostEqual(), pytest.assertTemplateUsed() etc).

@blueyed
Copy link
Contributor Author

blueyed commented Jun 9, 2014

@rafales
Thanks! That works great (except for another layer of confusion for code completion.. ;)

This should get added to http://pytest-django.readthedocs.org/en/latest/helpers.html.

@pelme
Copy link
Member

pelme commented Jul 26, 2014

Hmm, I think this should be namespaced somehow, like

pytest.django.assertTemplateUsed or something like that.

Also, generic helpers such as assertAlmostEqual might be nice to have, but it is not really the job of pytest-django to provide them.

@pelme
Copy link
Member

pelme commented Jul 26, 2014

Some more ideas: it should be possible to introspect SimpleTestCase and only add the assertion methods that are defined exactly at SimpleTestCase and not in its super classes.

@bittner
Copy link
Contributor

bittner commented Apr 18, 2015

I've tried to make PR #144 work opening the new PR #232, where I also merged in the current master to resolve merge conflicts in the PR.

Currently, the code doesn't seem to provide the assertXxx boilerplate functions via simple assert statements (see what Travis-CI reports). Can you guys help me understand what needs to be changed? -- How is this done in py.test? (I've not yet understood how the assert magic is done.)

I'm wondering whether the code in the original PR ever worked. GitHub reports it did (via Travis-CI integration), but the related test runs don't exist on Travis-CI. 😟

@blueyed
Copy link
Contributor Author

blueyed commented Apr 18, 2015

@bittner
The last build of #144 is: https://travis-ci.org/pytest-dev/pytest-django/builds/31046637 (you need to fix the URL, the repo has been renamed).

@blueyed
Copy link
Contributor Author

blueyed commented Oct 4, 2015

Closing this in favor of #232 (which is a continuation of this one).

@blueyed blueyed closed this as completed Oct 4, 2015
shepherdjay added a commit to shepherdjay/tdd that referenced this issue Apr 20, 2018
blueyed added a commit that referenced this issue Jan 13, 2020
beyondgeeks pushed a commit to beyondgeeks/django-pytest that referenced this issue Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants