python: pytest for cmd tools - python

I created a Python library and a set of Python script around it. An example of this small script could be something like this rna_ex2x.py:
./rna_ec2x.py
usage: rna_ec2x.py [-h] [--sep SEP] [--chain CHAIN] [--ec-pairs]
[--ss-pairs SS_PAIRS] [--pairs-delta]
interaction_fn
rna_ec2x.py: error: too few arguments
I want to test these script with pytest. I know how to test my functions with pytest, but I can't find in the documentation what would be the best practice in testing standalone Python script. Any suggestions?

I don't know what would be the best practice but I simply call my programs using subprocess.call(), check the result code and verify that the program did what it intended to do. See my tests as examples.

Related

iteratively and interactively writing and testing python code

I'd like to iteratively write and test my python code in the Python console. I can't find a way to easily load what i've got written in the editor (somefile.py) into the Python console in Pycharm. Is there a trick to doing so?
Pycharm will let me run the entire script but that's not useful because i want to build up my state in the shell by experimenting with the functions and data i've got in the environment (kind of like how a lisp programmer would use a REPL).
Pycharm have this alt-shift-E option.
This way you can write in the editor, select the lines you want to run and run them by alt-shift-E.
I use it all the time, although I find a lot of people not fameliar with this option.

How can I run a Jython script with Python

I am programming in Python(2.7), processing a bunch of data.
And I've got a software, what I have to use, and I want to start it automatically, and fill it up with data.
The problem is, that I cant open it with Python, because it has API only for Jython.
My question is, that how could I run a Jython script from a Python code(actually I am working on a standalone software)?
Is it even possible?
If it is, could you please give me a short example?
How to install Jython and how to run a file from python?
You can simply use Jython to run everything.
Having my_script.py and jython_script.py edit my_script.py by adding import jython_script and adding call jython_script.some_function().
# my_script.py
import jython_script
def my_function_using_some_function_from_jython_script():
...
jython_script.some_function()
...
Then simply call:
jython my_script.py
I assume you do not use modules that work only with CPython.

Python pdb on python script run as package

I have a python program that I usually run as a part of a package:
python -m mymod.client
in order to deal with relative imports inside "mymod/client.py." How do I run this with pdb - the python debugger. The following does not work:
python -m pdb mymod.client
It yields the error:
Error: mymod.client does not exist
EDIT #1 (to address possible duplicity of question)
My question isn't really about running two modules simultaneously python, rather it is about how to use pdb on a python script that has relative imports inside it and which one usually deals with by running the script with "python -m."
Restated, my question could then be, how do I use pdb on such a script while not having to change the script itself just to have it run with pdb (ie: preserving the relative imports inside the script as much as possible). Shouldn't this be possible, or am I forced to refactor in some way if I want to use pdb? If so what would be the minimal changes to the structure of the script that I'd have to introduce to allow me to leverage pdb.
In summary, I don't care how I run the script, just so long as I can get it working with pdb without changing it's internal structure (relative imports, etc) too much.
I think I have a solution.
Run it like this:
python -m pdb path/mymod/client.py arg1 arg2
that will run it as a script, but will not treat it as a package.
At the top of client.py, the first line should be:
import mymod
That will get the package itself loaded.
I am still playing with this, but it seems to work so far.
This is not possible. Though unstated in documentation, Python will not parse two modules via the -m command line option.

How to run Python & Behave tests with some unit test framework?

I need to run my tests (written with Python and Behave) without using console. I prefer to create simple Python script and use some unit test runner. I'm thinking about unittest, but pytest and nose solutions are also welcome:) I couldn't find any hint on behave's homepage.
behave is the "test runner". Use the "-o " option to store your results somewhere for whatever format(her) you want to use.
NOTE:
It is basically the same like py.test.

How to use `pytest` from Python?

I'm working in a project that recently switched to the pytest unittest framework. I was used to calling my tests from Eclipse, so that I can use the debugger (e.g. placing breakpoints to analyze how a test failure develops). Now this is no longer possible, since the only way to run the tests is via the command line blackbox.
Is there some way to use pytest from within Python, so that one is not forced to drop out of the IDE? The tests should of course not be run in a separate process.
I think I can now answer my own question, it's pretty simple:
import pytest
pytest.main(args)
which is documented in the Section "Calling pytest from Python code".
Then I can run this module and/or start it with the integrated debugger.
args is the list of command-line arguments, so for example to run only particular tests I can use something like:
args_str = "-k test_myfavorite"
args = args_str.split(" ")
pytest.main(args)
It seems that now (py.test version 2.0+) someone can also do this :
import pytest
pytest.main('-x {0}'.format(argument))
# Or
# pytest.main(['-x', 'argument'])
Ref
This is now supported by pytest and described nicely in the documentation.
You can invoke pytest from Python code directly:
import pytest
pytest.main()
this acts as if you would call “pytest” from the command line. It will not raise SystemExit but return the exitcode instead. You can pass in options and arguments:
pytest.main(["-x", "mytestdir"])
For me it was this:
pytest.main(["-x", "path to test file", "args"])
For example:
import pytest
pytest.main(["-x", "/api/test", "-vv"])
Maybe you could give a try to pycharm it has direct integration with py.test (I use it at work) and debugger runs perfectly.
I have not tried with eclipse, but as was suggested in a related question, it is possible to use the --pdb command line option with py.test. Maybe it is possible to configure eclipse that way.
However, calling the standard import pdb;pdb.set_trace() will not directly call the debugger. First it will issue an error which in turn will activate the debugger. This might or might not make things work differently.
You can just run py.test --pdb if you just want to a debugger and don't need the IDE

Categories