A talk without solutions
def test_logged_in_user_has_first_name():
# Arrange
test_name = "Bob"
user = User.objects.create(
first_name=test_name,
email="foo@excella.com"
)
# Act
result = user.first_name
# Assert
assert result == test_name
def test_logged_in_user_has_first_name():
# Arrange
test_name = "Bob"
user = User.objects.create(
first_name=test_name,
+ new_required_field="derp",
email="foo@excella.com"
)
# Act
result = user.first_name
# Assert
assert result == test_name
The fixture pattern:
Define a bunch of sample data in one place (e.g. JSON, raw SQL, etc.) that is intended to be reused by tests
[
{
"model": "my.app.user",
"pk": 1,
"fields": {
"first_name": "Bob",
+ "new_required_field": "foo",
"email": "foo@excella.com"
}
}
]
The fixture pattern:
Define a bunch of sample data in one place (e.g. JSON, raw SQL, etc.) that is intended to be reused by tests
Pros:
When the data model changes, you only have to change it in one place
Cons:
The dump a database pattern:
Periodically dump data from a deployed environment to keep the test fixtures up to date
Pros:
Cons
The factory pattern:
Use a tool like FactoryBoy to generate models dynamically using "realish" data
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = 'my.app.User'
first_name = 'john'
email = factory.Faker("email", domain="excella.com")
def test_logged_in_user_has_first_name():
# Arrange
test_name = "Bob"
- user = User.objects.create(
+ user = UserFactory(
first_name=test_name,
- new_required_field="derp"
- username="foo"
)
The factory pattern (continued):
Pros:
Cons:
Presented by Pandy Knight
Sunday 3:45 p.m.–4:15 p.m. EST