1

So I've got "Services" in my system that handle creating, updating etc of the data.

For example state_service.create() would create a new state in the database. This state belongs to a group.

The problem is, the group needs to be created first, before the state can be added.

Now, I can always call group_service.create() first, then create the state after, like this:

def test_state_service_create(self):
    self.group_service.create()
    state_id = self.state_service.create()
    self.assertTrue(state_id)

however I am not sure if this is bad practice, since this now relies on group_service.create() working correctly. The alternative is to manually create the group myself, such as:

def test_state_service_create(self):
    self.db.groups.insert_one(self.mock_groups[0])
    state_id = self.state_service.create()
    self.assertTrue(state_id)

But this would just mean that I'd need to update the mock data should the schema change.

Which is the proper practice here, or is it something else entirely?

3
  • 1
    Aren't you, in both examples, calling something that is not intended to be tested?
    – Theraot
    Feb 9, 2020 at 16:02
  • 1
    I'm perfectly fine with the first case. I've never understood the "relies on x working correctly". Of course it does. That is why you test that first.
    – Euphoric
    Feb 9, 2020 at 16:08
  • @Theraot, well the first example uses a method that's going to be tested as part of these test suites. The second example uses an external library that's tested independantly. I guess my thinking is that by using the second example, the test is less likely to fail for something else it's relying on.
    – Slepton
    Feb 9, 2020 at 16:23

1 Answer 1

1

IMHO it's perfectly fine to make whatever calls you need to prepare your test pre-requisite conditions - they are necessary to run your test.

If your testing framework has test skipping capabilities and if you have ways to check if those calls failed - i.e. if your test's pre-requisite conditions are NOT met then I'd also recommend skipping the test - you can't actually run it, so technically it'd be incorrect to declare it failed.

Immediately skipping tests on any failure in setting up pre-requisite conditions has some advantages:

  • reducing unnecessary noise, depending on how the project deals with test failures
  • potentially significant savings in expensive/lengthy testing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.