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!
source share