μλ νμΈμ, pytest-django κ°μ¬ν©λλ€! λͺ¨λ λ Έλ ₯μ κ°μ¬λ립λλ€. λ¬Έμ μμ΄ κ½€ μ¬λ¬ λ² μ¬μ©νμ΅λλ€.
μΌλΆ νλͺ©μ μ
κ·Έλ μ΄λνλλ° κ°μκΈ° mail.outbox
μμ ν
μ€νΈ μ€μ μ΄λ©μΌμ 보κ΄νμ§ μλ λ¬Έμ κ° λ°μνμ΅λλ€. λ΄κ° μ¬μ©νλ λ°©λ²μ μ νν μμ λ΄κΈ° μν΄ λ
Έλ ₯νμ΅λλ€ mailoutbox
μ€λͺ
λλλ‘ κ³ μ μ μ¬κΈ° νμ§λ§μ΄λ₯Ό μ¬μ©νλ λ° λ¬Έμ κ° μμ΄μ.
μ§κΈ μ°λ¦¬λ ν΄λμ€μμ ν
μ€νΈ μ€μ΄λ©° mailoutbox
κ³ μ μ₯μΉλ₯Ό μ λ¬νκΈ° μν΄ κ½€ λ§μ ꡬμ±μ μλνμ΅λλ€. μ¦, λλ iniλ₯Ό μ
λ°μ΄νΈνμ΅λλ€.
[pytest]
usefixtures = mailoutbox
κ·Έ κ³ μ μ₯μΉλ₯Ό μ¬μ©νλ λ°©λ²μ μ νν μμ§ λͺ»νμ§λ§ ν΄λμ€ λ©μλμ μμΉ μΈμ mailoutbox
λ₯Ό μΆκ°νλ €κ³ μλνμ§λ§ μ¬μ ν μ‘μΈμ€ν μ μμ΅λλ€. κ°λ¨ν κ²μ΄ λλ½λ κ² κ°μ΅λλ€.
μ¬λ°λ₯Έ λ°©ν₯μΌλ‘ μΆμ§ν΄ μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€. μ΄ νλ‘μ νΈμ λ€μ ν λ² κ°μ¬λ립λλ€!
https://github.com/pytest-dev/pytest-django/blob/5da0935731d71aa347c57cd1753f51e3ba9f32d5/docs/helpers.rst#clearing -of-mailoutbox(7aee367)λ₯Ό μ°Έμ‘°
(λ¬Έμ λ₯Ό μ λλ‘ μ΄ν΄νκ³ μλμ§ νμ€νμ§ μμ)
λλ κ°μ λ¬Έμ κ° μλ€κ³ μκ°νλ―λ‘ ν μ€νΈλ₯Ό κΈ°λ³Έ μ΄λ©μΌ μ μ‘μΌλ‘ μ€μ΄κ³ κ°λ³μ μΌλ‘ μ€ννκΈ°λ‘ κ²°μ νμ΅λλ€.
def test_reset_password(mailoutbox, db, settings):
text_message = render_to_string('emails/password_reset/password_reset_successful.txt',
context={})
html_message = render_to_string('emails/password_reset/password_reset_successful.html',
context={})
subject = render_to_string('emails/password_reset/password_reset_successful_subject.txt',
context={})
email = EmailMultiAlternatives(
subject=subject,
body=text_message,
from_email="[email protected]",
to=['[email protected]']
)
if html_message:
email.attach_alternative(html_message, "text/html")
email.send()
print(settings.EMAIL_BACKEND)
assert len(mailoutbox) == 1
> assert len(mailoutbox) == 1
E assert 0 == 1
E + where 0 = len([])
accounts/tests/api/test_reset_password.py:200: AssertionError
----------------------------------------------- Captured stdout call ------------------------------------------------
django.core.mail.backends.locmem.EmailBackend
--------------------------------------------- Captured stdout teardown ----------------------------------------------
μ΄μ λ©μ§ λΆλΆμ db
κ³ μ μ₯μΉλ₯Ό μ κ±°νλ©΄ ν
μ€νΈκ° ν΅κ³Όνλ€λ κ²μ
λλ€. κ·Έλμ django_db_setup
λ©μλλ₯Ό μμ νλ €κ³ μλνμ§λ§ μ무 μμ©μ΄ μμμ΅λλ€ ...
μ§κΈ λ§νμ΄μ.
mailoutbox
κ³ μ μ₯μΉκ° μλνλ κ²μ²λΌ 보μ΄λ λμΌν νλ‘μ νΈμ λͺ κ°μ§ ν
μ€νΈκ° μλ€λ κ²μ μμ΄λ²λ ΈκΈ° λλ¬Έμ ν΄λΉ ν
μ€νΈμ μ€ν¨ν ν
μ€νΈ μ¬μ΄μ μ°¨μ΄μ μ μ°Ύμλ³΄λ €κ³ νμ΅λλ€.
λ΄κ° μ°Ύμ μ μΌν μ°¨μ΄μ μ ν
μ€νΈ ν¨μμμ mailoutbox
μΈμμ μμΉμ
λλ€. db
κ³ μ μ₯μΉμ μμ‘΄νλ κ³ μ μ₯μΉ μμ μμΌλ©΄ μλνμ§ μμ§λ§ λ€μ λ£μΌλ©΄ μλνλ―λ‘ ν΄κ²° λ°©λ²μ μ°Ύμ κ² κ°μ΅λλ€.
pytest-django==3.3.3μμλ μ΄ λμμ λ³΄κ³ μμμ νμΈν μ μμ΅λλ€.
@bogdanpetrea
μ‘°μ¬/μ
λ°μ΄νΈμ κ°μ¬λ립λλ€.
λκ΅°κ° λλ²κΉ
/μμ νλ λ° λμμ΄ λ κ²μ
λλ€.
μ€μ λ‘ κ΄λ ¨μ΄ μλμ§ νμ€νμ§ μμ§λ§ mail.outbox
κ° μ²μμ μ€μ λμ§ μμ AttributeErrorκ° μμ μ μμμ λ°κ²¬νμ΅λλ€. νμ§λ§ μ΄κ²μ pytest-djangoκ° νμ νλ λμ νκ²½μ λ³κ²½νλ κ²κ³Ό κ΄λ ¨μ΄ μμ΅λλ€.
μ°Έμ‘°: https://github.com/pytest-dev/pytest-django/pull/708
λλ νμ¬ λ¬Έμ κ° μμ΅λλ€. κ·Έ μ°νΈν¨μ λΉμ΄ μμ§ μμ΅λλ€ :(
μ΅μ ν
μ€νΈ ν¨μ μΈμμ
λλ€.
νΈμ§: μ€μ ν
μ€νΈ μ½λ μ μ mailoutbox.clear()
λ₯Ό νΈμΆν©λλ€.
@μ λ
μ¬ν κ°λ₯ν ν
μ€νΈ μΌμ΄μ€λ₯Ό μ 곡ν μ μμ΅λκΉ? λ¬Όλ‘ pytest-djangoμ λν μ€ν¨ν ν
μ€νΈκ° κ°μ₯ μ’μ΅λλ€.
#708 κ΄λ ¨? (μ¦, κ±°κΈ°μ λΏλ μ½λ μ£Όμλ₯Ό μ°λ₯΄μμμ€)
λ¬Έμ λ₯Ό μ¬ννλ κ°λ¨ν ν μ€νΈ μ¬λ‘λ₯Ό λ§λ€μμ΅λλ€(μ μ΄λ λ΄κ° κ²ͺκ³ μλ λ°©μ): https://github.com/pytest-dev/pytest-django/compare/master...koniiiik :589- mailoutbox-is-not-django-core-mail-outbox?expand=1
mailoutbox
μ¬μ©ν λ°©λ²μμ mailoutbox.clear()
μ€μ λ‘ mailoutbox
λ° mail.outbox
λͺ¨λ μ§μΈ μ μμΌλ©΄ λ§€μ° λμμ΄ λ©λλ€. mailoutbox is mail.outbox
μΈ κ²½μ° λκ΅°κ°κ° μ½λλ₯Ό μ€ννκΈ° μ μ λ³΄λΌ νΈμ§ν¨μ μ§μΈ μ μμΌλ©° μ΄ μ½λλ νΉμ μμ μ΄λ©μΌμ μμ±νλλ‘ λμ΄ μμ΅λλ€(IEλ 1
μ΄λ©μΌ).
μ΄κ²μ΄ μ΄ λ¬Έμ μ κ΄λ ¨μ΄ μλμ§ νμ€νμ§ μμ§λ§ mailoutbox
κ³ μ μ₯μΉκ° λ΄ ν
μ€νΈμμ mail.outbox
μ λμκ³Ό μΌμΉνμ§ μμ΅λλ€. λ³΄λΈ μ΄λ©μΌμ μμ§νμ§ μμ΅λλ€.
(Pdb) mail.outbox
[<django.core.mail.message.EmailMultiAlternatives object at 0x7f6d864cb198>]
(Pdb) mailoutbox
[]
(Pdb)
μ΄κ²μ΄ μ΄ λ¬Έμ μ κ΄λ ¨μ΄ μλμ§ νμ€νμ§ μμ§λ§
mailoutbox
κ³ μ μ₯μΉκ° λ΄ ν μ€νΈμμmail.outbox
μ λμκ³Ό μΌμΉνμ§ μμ΅λλ€. λ³΄λΈ μ΄λ©μΌμ μμ§νμ§ μμ΅λλ€.(Pdb) mail.outbox [<django.core.mail.message.EmailMultiAlternatives object at 0x7f6d864cb198>] (Pdb) mailoutbox [] (Pdb)
κ°μ.
mail.outbox
μλ λ©μμ§κ° ν¬ν¨λμ΄ μκ³ mailoutbox
λ λΉμ΄ μμ΅λλ€.
pytest-django λ²μ μ 3.9.0μ
λλ€.
κ°μ λ¬Έμ κ° μμ΅λλ€.
λ€μκ³Ό κ°μ΄ κ³ μ μ₯μΉλ₯Ό μ¬μ©νλ©΄ μ€ν¨ν©λλ€.
def test_send_foo_mail(mailoutbox, user_client, foo):
λ€μκ³Ό κ°μ΄ μλν©λλ€.
def test_send_foo_mail(user_client, foo, mailoutbox):
κ·Έκ²μ λ°κ²¬νλ λ° λ§μ μκ°μ΄ κ±Έλ Έμ΅λλ€. μ΄ λ¬Έμ λ₯Ό λλ²κΉ νλ λ° λμμ΄ λμμΌλ©΄ ν©λλ€. ꡬνμ 보μμ§λ§ μ΄κ²μ λλ²κΉ νλ λ°©λ²μ λν λ¨μκ° μμ΅λλ€.
κ΄λ ¨: https://stackoverflow.com/questions/66846621/mailoutbox-works-only-if-last-fixture
μ΄μ ν΄μ€μμ λμΌν νλμ κ²½νν©λλ€. λ€μκ³Ό κ°μ΄ μ¬μ©νλ©΄ μλνμ§ μμ΅λλ€.
def test_command_and_email(
mailoutbox, argument, time_machine, mocker
):
κ·Έλ¬λ λ§μ§λ§ μμΉμ λ£μΌλ©΄ λͺ¨λ κ²μ΄ μμλλ‘ μλν©λλ€.
def test_command_and_email(
argument, time_machine, mocker, mailoutbox
):
μΈμ argument
λν μ¬μ©νλ기ꡬμ΄λ€ db
Iκ°μ΄ λ¬Έμ μ μ£Όλ μ΄μ κ°μμ, κ·Έλμ κ³ μ ν©λλ€.
pytets-django 4.3.0
κ°μ₯ μ μ©ν λκΈ
λλ κ°μ λ¬Έμ κ° μλ€κ³ μκ°νλ―λ‘ ν μ€νΈλ₯Ό κΈ°λ³Έ μ΄λ©μΌ μ μ‘μΌλ‘ μ€μ΄κ³ κ°λ³μ μΌλ‘ μ€ννκΈ°λ‘ κ²°μ νμ΅λλ€.
μ΄μ λ©μ§ λΆλΆμ
db
κ³ μ μ₯μΉλ₯Ό μ κ±°νλ©΄ ν μ€νΈκ° ν΅κ³Όνλ€λ κ²μ λλ€. κ·Έλμdjango_db_setup
λ©μλλ₯Ό μμ νλ €κ³ μλνμ§λ§ μ무 μμ©μ΄ μμμ΅λλ€ ...μ§κΈ λ§νμ΄μ.