Testing Python Modules

I wrote a script that opens a file, reads the contents and performs some operations and calculations and saves them in sets and dictionaries.

How do I write unit test for such a thing? My questions specifically:

  • Would I check that the file is open?
  • The file is huge (this is a unix dictionary file). How would I unit test calculations? Do I have to literally manually calculate everything and verify that the result is correct? I have the feeling that this defeats the whole purpose of unit testing. I do not take any part through stdin.
+4
source share
3 answers

This is not what unit testing is!

  • Your file does not represent UNIT, so you are not testing the file or the WITH!
  • your unit test should check every method of your functions / methods that processes: a) file processing; b) calculations
  • It’s not uncommon that your unit tests exceed the code line of the devices you are testing.

Unit testing means (not a complete, not a bike definition):

  • minimalistic / atomic - you can separate your units to the most elementary / simple device; the device is usually called (method, function, called object)
  • separation of anxiety - you test ONE and only ONE thing in each individual test; if you want to test different conditions of one device, you write different tests.
  • determinism - you give the unit something to process, with a preliminary understanding that this result MUST be
  • If your device does not have enough knowledge, a specific environment is required, you create a device / test setup / layout
  • unit-tests (usually) are incredibly fast! if he slowly checks that you have violated another point on top
  • If you need to check something that breaks something above, you may have taken the next step in testing for integration tests
  • you can use the unit test framework for non-unit tests, but do not call it a unit test just because of the use of unittest-framework

This guy (Gary Bernhardt) has some interesting practical examples of what testing and unit testing mean.

Update for some clarification:

"1. Would I check that the file is open?"

Well, you could do it, but why would it be "UNIT"? Keep in mind that the test has only two solutions: pass and fail. If your test fails, it should (ideally should) have only one reason: your block (= function) sucks! But in this case, your test may fail, because: * the file does not exist * is locked * is damaged * The absence of processed files * from memories (large file) * moon phase, etc.

so what would the failure (or passing) of the "unit" test say about your device? You do not test your device alone, but all the surrounding environment. This is another system test! If you want to test inactivity to successfully open a file, you must at least mock the file.

"2 ... How would I unit test the calculations? Should I literally manually calculate everything and check the correctness of the result?"

No. You should write a test for angular and ordinary cases and check the expected result compared to the processed one. The number of tests required depends on the complexity of your calculations and exceptions to the rule.

eg:.

def test_negative_factor(self): assert result def test_discontinuity(self): assert raise exception if x == undefined_value 

I hope I made myself clearer!

+14
source

You must reorganize your code to verify that it is working. This, at the top of my head, will say:

  • Take the functionality of opening a file in a separate block. Have the new block get the file name and return a stream of content.
  • Make your block the receiver of the stream and read it, instead of opening the file and reading it.
  • Write unit test for your main (calculated) device. You will need to make fun of the stream, for example. from the dictionary. Write a few test cases, each time your block is provided with a different thread, make sure that your block calculates the correct data for each input.
  • Get your code coverage as close to 100% as possible. Use nosetests to cover.
  • Finally, write a test for your "stream provider". Submit it with several files (save them in the test folder) and make sure that your streaming provider reads them correctly.
  • Get a second unit test coverage as close to 100% as possible.
  • Now, and only now, commit your code.
+5
source

You have not explained what computation is, but I believe that your program should also work with a subset of a large file. If so, do a unit test, which opens a small file, does the calculation and gives some result that you can check correctly.

0
source

Source: https://habr.com/ru/post/1447825/


All Articles