2

I have a few functions for performing actions in python, and I am debating whether or not it makes sense to write unit tests for these.

There is one function that resolves a host using the socket module. I don't think it makes sense to write a test for the socket module itself, but seems like it may make sense to write a functional test to see if DNS is working.

I have another function that opens a file and puts the contents into a list.

Does anyone have any guidance or links on performing unit tests/functional tests for items like this? Most of what I have read pertains to application level testing only.

Thank you in advance.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
fr00z1
  • 519
  • 5
  • 16

1 Answers1

3

First of all, if you don't have tests at all, better start with high-level end-to-end functional tests and end with unit tests gathering coverage statistics on every new test you write.

While writing a test to a function which uses system or network libraries, you usually want to isolate your test, to make it independent and straight as much as possible by mocking out system and network calls (see Mock library).

By using mock library you can and should test how does your application/function respond to situations where there is a socket or system error.

Also see:

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195