---
slug: "django-unittest-expected-actual"
title: "Django の ユニットテストの assertEqual の引数の first, second は実際は expected, actual"
description: "Django のユニットテストの assertEqual メソッドを見てみると、第一引数は first, 第二引数が second と命名されており、それぞれの変数に用途の違いは無いように見える。"
url: "https://www.ytyng.com/blog/django-unittest-expected-actual"
publish_date: "2024-03-28T00:41:49Z"
created: "2024-03-28T00:41:49Z"
updated: "2026-02-26T07:19:52.063Z"
categories: ["Django", "Python"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250611/f538129f31c34a84a4676b587317fb54.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/304/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/304/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/304/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/304/featured-music-304-3.mp3", "https://media.ytyng.net/ytyng-blog/304/featured-music-304-4.mp3"]
lang: "ja"
---

# Django の ユニットテストの assertEqual の引数の first, second は実際は expected, actual

Django のユニットテストの assertEqual メソッドを見てみると、

```python
def assertEqual(self, first, second, msg=None):
    """Fail if the two objects are unequal as determined by the '=='
       operator.
    """
    assertion_func = self._getAssertEqualityFunc(first, second)
    assertion_func(first, second, msg=msg)
```
第一引数は first, 第二引数が second と命名されており、それぞれの変数に用途の違いは無いように見える。

ただし、下記のようなコードを書いて

```python
from django.test import TestCase


class AssertTest(TestCase):
    def test_assert_equal(self):
        self.assertEqual(
            'expected',
            'actual'
        )
```

実際にテストが失敗すると、結果に

```
Failure
Expected :'expected'
Actual   :'actual'
```

と出力されるので、第一引数が Expected, 第二引数が Actual だと思うべきです。

そのため、ユニットテストにハードコーディングする値は Expected なので第一引数、
メソッド実行結果や結果の変数は Acutal なので 第二引数になる。

```python
self.assertEqual(
    110,
    get_tax_included_price(100)
)
```

にもかかわらず、実際の Django のユニットテストの例を見ると逆なので、実態としてどっちでも良い。

本当は [Black](https://github.com/psf/black) あたりがどっちかに決めてくれると良いと思う。
