test in python

tl;dr

I’ve reorganized Photochooser a lot, changing names and directories. Also wrote my first test in Python, ugly one but still. The code is here.

Motivational kick

A couple days ago my project was mentioned (it’s in Polish) by one of the contestants of GetNoticed competition. And it was a very strong motivational kick! Until then I was writing Photochooser just for myself, didn’t bother if anyone like it or not. But now, when I know someone looked at my project and found it potentially useful? It changes everything 😉 I really want to do it right, write useful application, pretty and tested code.

Thank you, Konrad!

The application

Previously I’ve created simple MVP and some mockups of Photochooser. So the next step should be making the real application looks and behaves like mockups.
Photochooser can change images now, so it’s pretty useful right now – on OS X the default images viewer cannot change images, or I just can’t find the way. To this moment I’ve used some app from the internet, but now I can use Photochooser. Nice 🙂
photochooser changing images

The code is still messy, but it slowly, but steadily evolves. I have a couple of methods instead of one. Because I want to write tests the next step will probably be splitting the code into several modules – the ones responsible for displaying and those responsible for logic.

Talking about testing…

My first Python test!

I wrote it! It is very simple and a bit ugly – it requires launching the application and performing some action, but it works and passes 😎

It’s worth mention that Python hasn’t standardized way to place tests. Some say that test should be placed next to tested module, some other place them inside tests directory. I’ve decided to use the latter, and create src directory for modules and tests for tests.

One more thing – to make module visible from the tests I’d to create empty file __init__.py next to it. I don’t know how it works exactly but without it, the module won’t be visible for another one, including tests.

The test itself is not really something worth mention, but writing it caused some serious changes in the code.

New structure

Before writing tests structure of the code looked like this

photochooser
⌙ photochooser.py

and now it looks much better

photochooser
⌙ src
    ⌙ app.py
⌙ tests
    ⌙ test_app.py

The structure is much flexible now, it’ll be easy to add more modules and tests without losing clarity. As you can see I’ve changed photochooser.py to app.py – this file will be the main module displaying GUI and so on, and the logic will be placed in another module. Separation of concerns.

What next?

I think my current way for creating the app – writing some feature and refactor and improve the code is the right one. So next time I’ll write maybe settings window, or implement SQLite database for storing data. Something like that. And maybe I’ll split the code into modules and write some tests.
Come back in a few days to see yourself 😉

If you want to dive into code, here it is, enjoy.

Want more #GetNoticed2017 posts?