last day (14 days later) » 

14:55
5
Q: How do I test a method that requires a file's presence?

ChuckFor the first time, I'm using Python to create a library, and I'm trying to take the opportunity in this project to learn unit testing. I've written a first method and I want to write some unit tests for it. (Yes, I know that TDD requires I write the test first, I'll get there, really.) The meth...

Have you considered mocking the object?
@Oasiscircle, that's the "I'm trying to take the opportunity to learn unit testing" idea. I don't understand mocking, or fixtures. Python tutorials on unit testing seem to assume that I understand these concepts, while unit testing tutorials tend to concentrate on a specific language, such as Java, which I'm not very conversant in. I guess I'm hoping someone can give me an example using Python's unittest module and whatever other built-in Python features I would need for mocking and fixtures.
I provided a link in my comment, I just figured I'd give you the language (I didn't know the mock module existed until a few years ago) to find potentially good resources. Maybe google "python mock examples".
@Oasiscircle I'll do that, but I'm at the point in ignorance of not knowing that what I'm looking for is mocking (well, until you suggested it :) ). If I understand mocking and its implementation in Python, does that solve my problem? I don't yet know what I don't know.
I believe that examining mock would greatly help you with this type of issue. Unit tests are so low level that it's beneficial to get abstract away any function that uses something on the OS (like the file system) and the mock module provides the functionality and framework for creating "fake" constructs (modules, objects, instances) that return pre-determined values allowing you to better test the interface of your design and how your interfaces work together. The proper time to actually test the internal workings of your functions is during CI tests. (Think Travis if you're on Github)
For instance, you can mock the os module's listdir() function to return a pre-determined amount for the single unit test without actually having to construct a directory with those files. Etc etc.
14:55
@Oasiscircle I'm reading some articles on Python mocking now, but it sounds like that might not test what I want it to. In testing this particular method, I want to ensure that given an archive, it returns the right number of contained files. Another test I'd want would be, given a non-existent file or a non-archive file, it returns an error. It sounds like mocking would by pass both of those because the objects are virtual? I might be misunderstanding this, of course, and perhaps reading these articles will help.
Hey there. Thank you very much for your time on this.
So the biggest thing I think that will help is that there are multiple levels of tests
Unit tests are super low-level, meaning they usually only deal with one object interface
By multiple levels, do you mean unit tests to test the interface and other tests to test the actual algorithms?
14:59
What would you call testing the algorithms then
?
So CI tests, which hopefully you outsource to travis or something, those ones can do all the creation and deletion and stuff
Integration tests?
Unit-tests are usually only testing one object or module
You can do simple tests in unittests without mocking, but once the OS is involved or stuff that takes a long time like requesting things from the network, etc
It's a good idea to mock them out, just to make testing small things quicker
I apologize, but this is testing only one object (I think). I'm only testing whether one method in one object is working correctly. Aren't I?
There should be at least one unit test per method
And if there's branching/decisions within that method, there should be one test that covers each decision
including exceptions.
OK, let me turn it around, given an ArchiveFile class that has a file_count method that depends on a file attribute, what would you say should be the "at least one unit test" for it?
And try to keep it down to one assert per test, but having multiple that are similar is okay. That makes it so that when a test fails you know EXACTLY what went wrong
Is the file attribute an actual file on the file system?
15:02
It's supposed to be. If it turns out it isn't, an exception should be raised.
Okay so you would mock out whatever module you're using to open the file or mock the file/open function to do what you want
Which in one case is return a mocked "file" object
and another is to raise an exception that the file isn't found
And then in the mocked file object you just mock the methods you use like read, close, etc.
The basic idea of mocking is to keep everything contained so that when something goes wrong we know it's our stuff not someone elses.
Hold on, launching MacVim, thought it was already open. Want to reference some of the source code for a moment...
OK, so the current file_count method depends on the zipfile library, the ZipFile class, and its namelist method. You're saying I would mock that?
If so, I think I'm starting to understand. :)
Only the relevant parts you need which is usually less than the complete package.
And it's not like you're doing actual work, you're just returning values you want.
15:07
Right, I'd only need to mock the namelist method of ZipFile.
the epiphany has arrived (I hope)
Then when you do a unittest run you don't care about the interface of zipfile, you just use it as you intend and test your own object's response to the interface
I'm glad :)
tyvm, I'll finish the article I'm reading, and look at one or two more, and then try diving back into the code.
Good luck, I'll keep the chat open for a while, just ping me.
15:09
Seriously, post this stuff as an answer. I'll edit the question to include the info about ZipFile and namelist.
I can guarantee you there's already an answer for this but I just didn't want to look for it ;P
I'm technically at work so can't be doing too many things at once.
Understood. I just edited the question to include my file_count method code.
Funnily enough I'm a QA engineer for Python, so I should probably answer the question... can you link it to me here I closed the tab.

last day (14 days later) »