How am I supposed to add tests for all my projects?

I have been teaching myself how to code for about a year (I’m 17) and one of the most common things that I’ve come across while reading articles about programming is automated tests for your code.

I know automated testing is something very helpful but I still don’t get it, what are you supposed to test in your code?

I’ve seen most of the tutorials on writing unit tests use assert_equal (in Python) to test two values.

Now for example I have this project which is somewhat a calculator, how am I supposed to write tests for the trigonometry part?

Is this the correct way of testing the script?

from src.trigonometry import calculate
def test_calculate():
    assert_equal(calculate("Degree", "Sin", 90), Math.sin(90 * Math.PI/180))
    # Similar assert_equals for other trig functions with angle 90

Another thing I am confused about is how am I supposed to write tests for scripts that interact with the web such as:

  • An IRC Bot
  • An Online Dictionary

5

Is this the correct way of testing the script?

assert_equal(calculate("Degree", "Sin", 90), Math.sin(90 * Math.PI/180))

This tests if your function behaves identical to the standard library. Not if it behaves the way you expect it to behave. It is better to hardcode the expected result:

assert_equal(calculate("Degree", "Sin", 90), 1.0)

how am I supposed to write tests for scripts that interact with the web?

Unit tests should be as independent from other components as possible.

When you have your unit-test go online and do a network request, you are not just testing your code, but also your internet connection and the service you interact with. That’s an integration test, not a unit test. The problem with such tests is that if they fail, you never know if it’s your code which is to blame, your internet connection which doesn’t work or an error in the service you interact with.

In a good unit test you minimize dependencies to other resources as much as possible. A common method for this are mock objects. A mock object is an object which looks and behaves identical to an object which abstracts an external resource (like a network connection or database connection object), but instead of actually doing a network request it just pretends to do one and returns a hardcoded result. This allows you to test if your code performs correctly without dependency on the 3rd party service.

In order to allow your unit tests to test your code with a mock network connection instead of a real one, you need to follow a development methodology called dependency injection. This methodology means that whenever an object has a dependency on a resource, it doesn’t acquire that resource itself but instead gets that object handed to it from the outside. This allows your production code to supply a real network connection and your test code to supply a mock network connection.

The nice thing about mock network connections is that you can implement many different ones with different behaviors. This allows you to implement tests for your error handling in the case that the server does not behave as it usually does. You can, for example, test how your code behaves when the server suddenly closes the connection, responds with an error message or responds with total garbage.

5

While not being a python developer, the tests shall be readable. Thus I’d suggest rewriting your test:

from src.trigonometry import calculate 
def test_calculate():

    expected = 0
    actual = calculate("Degree", "Sin", 90)

    assert_equal(expected, actual)

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *