# Mocking ORM to speed up tests?

**URL:** <https://forum.djangoproject.com/t/mocking-orm-to-speed-up-tests/6847>\
**Category:** Using Django\
**Created:** [March 2, 2021, 9:34am UTC](https://forum.djangoproject.com/t/mocking-orm-to-speed-up-tests/6847 "2021-03-02T09:34:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guettli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/guettli/32/1896_2.png) [@guettli](https://forum.djangoproject.com/u/guettli)\
**Post date:** [March 2, 2021, 9:34am UTC](https://forum.djangoproject.com/t/mocking-orm-to-speed-up-tests/6847/1 "2021-03-02T09:34:46Z")

</div>

Our tests are getting slow, and if I profile them, I see that a lot of time is spent  
inside the database calls.

Rewriting the tests is a lot of effort.

Is there an easy solution like an in-memory ORM?

We use PostgreSQL in development and production.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 2, 2021, 10:06am UTC](https://forum.djangoproject.com/t/mocking-orm-to-speed-up-tests/6847/2 "2021-03-02T10:06:08Z")

</div>

In-memory ORM’s don’t really work since the Django ORM is really an SQL generator. It would also prevent you from using more advanced features.

You can speed up PostgreSQL by mounting it on an in-memory filesystem. In docker-compose this is as easy as:

```auto
...
  services:
    postgres:
      image: postgis/postgis:12-3.0
      ports:
      - 5432:5432
      volumes:
      - type=tmpfs,destination=/var/lib/postgresql/data

```

The other thing you can do is reduce the amount of database usage in your tests. A key way to achieve this is to use `setUpTestData`, alongside making sure you use `TestCase` instead of `TransactionTestCase` as much as possible.

There’s a lot to cover here… so much I [wrote a book](https://gumroad.com/l/suydt)… 😉

---

<div class="post-metadata">

**Author:** ![mblayman](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mblayman/32/101_2.png) [@mblayman](https://forum.djangoproject.com/u/mblayman)\
**Post date:** [March 3, 2021, 5:23am UTC](https://forum.djangoproject.com/t/mocking-orm-to-speed-up-tests/6847/3 "2021-03-03T05:23:10Z")

</div>

Adam’s book is great. IMO, it’s a no brainer to pick up if you’re working at a company that uses Django with an unoptimized test suite. And even if your company’s test suite is pretty optimized, there are some great tips in the book that would likely improve the performance further.

---

<div class="post-metadata">

**Author:** ![guettli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/guettli/32/1896_2.png) [@guettli](https://forum.djangoproject.com/u/guettli)\
**Post date:** [March 3, 2021, 7:29am UTC](https://forum.djangoproject.com/t/mocking-orm-to-speed-up-tests/6847/4 "2021-03-03T07:29:15Z")

</div>

I bought the book and it contains a lot of things which are new to me, although I use django since several years .Thank you for the book.

Maybe you have a solution to this fundamental question:

> <https://stackoverflow.com/questions/66225251/pytest-interactive-output-instead-of-pure-ascii>

Reading the the ascii output of pytest daily feels like in stone-age and pytest-html is not much better.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 3, 2021, 1:07pm UTC](https://forum.djangoproject.com/t/mocking-orm-to-speed-up-tests/6847/5 "2021-03-03T13:07:28Z")

</div>

Thank you!

Personally I feel fine with the default pytest output. But I can see how it would be useful.

PyCharm has a graphical test runner that I believe integrates with pytest.

On the terminal, some advanced terminal emulators like iTerm support extra [escape sequences](https://iterm2.com/documentation-escape-codes.html) which may allow adding enhanced functionality there. I know [pytest-travis-fold](https://pypi.org/project/pytest-travis-fold/) uses similar escape sequecnes to render folding sections on Travis.
