Development work flows
I am playing about with writing a Telegram Bot and Pythonista is working great, I love this app. I am just missing one thing now things are getting more complex: how do I run my code in a server on my machine for testing, I want to be able to input a value and get a response from the program without having to keep pressing the play button. I know some people on here are building some serious stuff and I can't believe they are doing this by running the script from start to finish each time?
Any help on some dev workflows to work round this would be really helpful, it's all I need to make Pythonista my main dev environment.
Whatevery you wrote, it's just Python code. It can be executed even if your code does contain shebang line
#!python3, etc. Here's the list of potential issues you can have.
Module is missing - set to None
Pythonista modules are not available outside of Pythonista. Like
One way to handle modules that are not available is to wrap
try/exceptand set the module to
Noneif it's not avaiable. See log.py for example. Actually it should be
except ImportErrorinstead of bare
except. And then just check if module was set or not.
Module is missing - Mock
Another way is to use
unittest.mock. If you do not want to check it against
None, you can do something like:
try: import console except ImportError: console = unittest.mock.MagicMock()
Now you can call whatever from
MagicMockhandles it and your code doesn't crash.
mockis useful not just for mocking non existing modules, but you can use it for other things like patching existing functions. You can even check if mocked method was called, check what arguments were passed, etc.
Basically, if you have a module/function which handles communication with some server, you can patch this module / function to avoid real communication and to return whatever you need for tests.
You can learn more here.
Here's an example how to write tests. It's simple, I just started, but you can get a clue. Tests like these are runnable from within Pythonista (
Cmd Uin Black Mamba or via play button).
And here's an example how to run style check & tests on Travis CI.
You'll probably see more issue than these mentioned ones, but it's a good starting point and it should solve major issues for you.