Can i use regex within a pytest expression - python

Is it possible to locate tests with pytest using pattern matching, for example i want to find all tests that begin with the letters from a-m
i have been trying things like
pytest -m ^[aA-mM]
pytest --collectonly -k test_^[aA-mM] --quiet
Not got it to work so far, is this possible?

Doesn't seem possible according to pytest doc.
Have you considered marking the tests instead?
This helps with filtering them out when you run pytest.
More info about marking could be found in the pytest doc about markers...
or another tutorial about it
But in short, for example:
just add #pytest.mark.foo onto some tests, and #pytest.mark.bar to others
run pytest -m foo to run the tests marked as foo only.

Related

Difference between "python3 -m test" and "python3 -m unittest"

I found out that I can discover and run unit tests under my directory tree by doing this:
python3 -m test
The above works, but the documented method to discover and run all tests finds hundreds more, including a new one that was not found by the previous method:
python3 -m unittest
What exactly is -m test and why can't I find documentation on it after a quick search, except the following page which seems to be about CPython?
https://devguide.python.org/runtests/
The test package is intended to test the Python API itself. According to the documentation:
Note: The test package is meant for internal use by Python only. It is documented for the benefit of the core developers of Python. Any use of this package outside of Python’s standard library is discouraged as code mentioned here can change or be removed without notice between releases of Python.
The link to this documentation appears in the TOC under Development Tools. While it is not entirely surprising that the python3 -m test command discovers and runs tests, it is not really designed to discover and run the tests that you write for your own code.

Running tests from coverage.py vs running coverage from test runner

During the Coverage.py with Ned Batchelder python&testing podcast, Brian and Ned briefly discussed that, if you need to run tests with coverage, it is preferred to run tests from coverage.py executing the coverage run as opposed to invoking a test runner with coverage. Why is that and what is the difference?
To put some context into this: currently I'm using nose test runner and execute the tests with the help of nosetests command-line tool with --with-coverage option:
$ nosetests --with-coverage --cover-html
Should I do it via the coverage run -m instead?
$ coverage run -m nose
$ coverage report
I guess I am uniquely qualified to answer this question :)
mwchase and mgilson have it right in their comments: using a plugin means you are depending on that plugin's behavior being correct and understandable. In the name of being helpful, plugins will have their own logic that may have been the best idea when they were written, but the test runner and/or coverage.py may have changed in the meantime. The plugins tend not to be as well-maintained as the other components. If you can avoid them, you have one less thing to think about.
True fact: the reason I added support for .coveragerc configuration files in the first place was because I wanted to add features to coverage.py and didn't want to wait for plugin UIs to be updated to support them.

python nose tests: is it possible to run a subset of tests

I'm using nose for testing some REST API written using Flask. Also I'm using script-manager. Everytime I do manage test it'll run through all the tests. This is OK for CI but not ideal if one wants to fix something. In golang, there is a way to specify a subset of test to run by providing a regexp. Is there something similar in nose?
You can run
nosetests -m REGEX
as specified in nose's options page.
If you don't need full regex you can also specify a path with grobs after nosetests, e.g.:
nosetests tests/my_cool_subset*

Python benchmark tool like nosetests?

What I want
I would like to create a set of benchmarks for my Python project. I would like to see the performance of these benchmarks change as I introduce new code. I would like to do this in the same way that I test Python, by running the utility command like nosetests and getting a nicely formatted readout.
What I like about nosetests
The nosetests tool works by searching through my directory structure for any functions named test_foo.py and runs all functions test_bar() contained within. It runs all of those functions and prints out whether or not they raised an exception.
I'd like something similar that searched for all files bench_foo.py and ran all contained functions bench_bar() and reported their runtimes.
Questions
Does such a tool exist?
If not what are some good starting points? Is some of the nose source appropriate for this?
nosetests can run any type of test, so you can decide if they test functionality, input/output validity etc., or performance or profiling (or anything else you'd like). The Python Profiler is a great tool, and it comes with your Python installation.
import unittest
import cProfile
class ProfileTest(unittest.TestCase):
test_run_profiler:
cProfile.run('foo(bar)')
cProfile.run('baz(bar)')
You just add a line to the test, or add a test to the test case for all the calls you want to profile, and your main source is not polluted with test code.
If you only want to time execution and not all the profiling information, timeit is another useful tool.
The wheezy documentation has a good example on how to do this with nose. The important part if you just want to have the timings is to use options -q for quiet run, -s for not capturing the output (so you will see the output of the report) and -m benchmark to only run the 'timing' tests.
I recommend using py.test for testing over. To run the example from wheezy with that, change the name of the runTest method to test_bench_run and run only this benchmark with:
py.test -qs -k test_bench benchmark_hello.py
(-q and -s having the same effect as with nose and -k to select the pattern of the test names).
If you put your benchmark tests in file in a separate file or directory from normal tests they are of course more easy to select and don't need special names.

unittest colored output

I use unittest (actually unittest2) for Python testing, together with Python Mock for mocking objects and nose to run all tests in a single pass.
I miss being able to tell what is working and what's wrong at a glance from the green/red bars. Is there a way to get colored output from unittest?
(Changing test suite at this point is not an option, and I actually like unittest)
Using a method very similar to robert's answer, I have (today!) released a package that enables colour output in unittest test results. I have called it colour-runner.
To install it, run:
pip install colour-runner
Then, where you were using unittest.TextTestRunner, use colour_runner.runner.ColourTextTestRunner instead.
See how it looks with verbosity=1...and verbosity=2
I'm having good success with nosetests and rednose. It's still maintained at the time of writing this.
In python 2.x you could try pyrg. Does not work in Python 3 though.
Make a class that inherits from unittest.TestResult (say, MyResults) and implements a bunch of methods. Then make a class that inherits from unittest.TextTestRunner (say, MyRunner) and override _makeResult() to return an instance of MyResults.
Then, construct a test suite (which you've probably already got working), and call MyRunner().run(suite).
You can put whatever behavior you like, including colors, into MyResults.
If you are running pytest this way:
python -m unittest test_my.py
Change it to:
pytest test_my.py
And you get colors for free
pytest can do this with no changes needed for unit tests.
Now install pytest.
pip install --user pytest
And run the tests to see the color!
If you could change just the line of your test imports, you could use redgreenunittest. It's a clone I made of unittest, but it has colorized output.
If you want to use it without updating any of the meat of your code, you can just use it like so:
import redgreenunittest as unittest
It's not a clone of unittest2, so it wouldn't work out-of-the-box with Andrea's code, but its source is right there, so a unittest2 fork of redgreenunittest wouldn't be out of the question.
Also, any "you're doing it wrong" comments are welcome, so long as they contain some reasoning. I'd love to do it right instead.
I've also found another colouring plugin for nose: YANC at https://pypi.python.org/pypi/yanc
Works for me with Python 3.5 and nose 1.3.7 (I couldn't get any of the other options for nose listed above to work)
Try rudolf plugin for nosetests.

Categories

Resources