Output Test names in unittest discover - python

I'm running Python unittest using the discover mode:
% python -m unittest discover
The system prints a dot for each test, but I'd rather see a test name.
Is there an option that makes this happen?

The verbose flag (-v) is what you're looking for:
$ python -m unittest discover -v
test_a (tests.test_a.TestA) ... ok
test_b (tests.test_b.TestB) ... ok
...
For more options, check:
$ python -m unittest --help
usage: python -m unittest [-h] [-v] [-q] [--locals] [-f] [-c] [-b]
[tests [tests ...]]
positional arguments:
tests a list of any number of test modules, classes and test
methods.
optional arguments:
-h, --help show this help message and exit
-v, --verbose Verbose output
-q, --quiet Quiet output
--locals Show local variables in tracebacks
-f, --failfast Stop on first fail or error
-c, --catch Catch Ctrl-C and display results so far
-b, --buffer Buffer stdout and stderr during tests
...

Related

Python pre-commit unittest faild

I wish pre-commit to run the tests before committing my code.
The command python -m unittest discover is working in the command line.
D:\project_dir>python -m unittest discover
...
...
...
----------------------------------------------------------------------
Ran 5 tests in 6.743s
OK
But when trying to commit I am getting
D:\project_dir>git commit -m "fix tests with hook"
run tests................................................................Failed
hookid: tests
usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
[-b] [-k TESTNAMEPATTERNS] [-s START]
[-p PATTERN] [-t TOP]
python.exe -m unittest discover: error: unrecognized arguments: bigpipe_response/processors_manager.py
usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
[-b] [-k TESTNAMEPATTERNS] [-s START]
[-p PATTERN] [-t TOP]
python.exe -m unittest discover: error: unrecognized arguments: tests/test_processors.py
Here is my .pre-commit-config.yaml file.
- repo: local
hooks:
- id: tests
name: run tests
entry: python -m unittest discover
language: python
types: [python]
stages: [commit]
Also for language I try to use system. I got the same result.
How can I solve this? Please help.
You can try the following YAML. Of course, you should change the pattern in args option if you are using different one.
- id: unittest
name: unittest
entry: python -m unittest discover
language: python
'types': [python]
args: ["-p '*test.py'"] # Probably this option is absolutely not needed.
pass_filenames: false
stages: [commit]
You should set to false the pass_filenames parameter because in other case the files will be passed as arguments and as you mentioned in your question these are "unrecognized" parameters.
The accepted answer won't work if your application depends on some packages. In this case, the language:python setting lets pre-commit use a custom virtual environment (within the ~/.cache folder), that you can add dependencies using the additional-dependencies flag. However this usually means copying the contents of 'requirements.txt`
So in order to use your application's virtual environment, you have to set the language:system setting and put your tests into the repo: local. Then the virtual environment that is activated when running the pre-commit hooks is used.
A working configuration would look like this:
- repo: local
hooks:
- id: unittests
name: run unit tests
entry: python -m unittest
language: system
pass_filenames: false
args: ["discover"]

How to override the cli name in argparse -h usage information?

I am bundling my python app into an .AppImage file. Now, when I run it with flag -h I would expect it to print something along these lines:
$ ./mytool.AppImage -h
usage: mytool [-h] [-d DIR] [-f] [-e] [BLA [BLA ...]]
...
But due to the nature of the AppImage bundling process I get:
$ ./mytool.AppImage -h
usage: AppRun [-h] [-d DIR] [-f] [-e] [BLA [BLA ...]]
...
That is, AppRun instead of mytool.
So my question is:
How can I force override the app name so that regardless of how the app is called it will always print the same name in the usage string?
As per hpaulj's comment, this can solved by simply setting the prog parameter of the argparse.ArgumentParser constructor:
parser = argparse.ArgumentParser(
prog='mytool',
description='Some description...'
)

--help for a selected command line argument

Is it possible to get --help message for a selected argument in command prompt?
Example:
python my_program.py -h in cmd will provide the whole help message.
Say my_program.py takes 2 cmdline argument -a and -b. Can I get the --help message of -a alone?
Something like this, python my_program.py -h -a. Is that possible?

How to run the code in an __init__.py

I have found some code that I think will allow me to communicate with my Helios Heat recovery unit. I am relatively new to Python (but not coding in general) and I really cannot work out how to use this code. It is obviously written for smarthome.py but I'd like to use it from the command line.
I can also see that the way this file is constructed is probably not the best way to construct an __init__.py but I'd like to try and use it first.
So, how do I run this code? https://github.com/mtiews/smarthomepy-helios
Cheers
After git clone https://github.com/mtiews/smarthomepy-helios.git: either
invoke python with the __init__.py script as argument:
python smarthomepy-helios/__init__.py
or
make the __init__.py executable and run it:
chmod u+x smarthomepy-helios/__init__.py
smarthomepy-helios/__init__.py
Running it either way gives me
2016-02-20 18:07:51,791 - root - ERROR - Helios: Could not open /dev/ttyUSB0.
Exception: Not connected
But passing --help I get some nice synopsis:
$> python smarthomepy-helios/__init__.py --help
usage: __init__.py [-h] [-t PORT] [-r READ_VAR] [-w WRITE_VAR] [-v VALUE] [-d]
Helios ventilation system commandline interface.
optional arguments:
-h, --help show this help message and exit
-t PORT, --tty PORT Serial device to use
-r READ_VAR, --read READ_VAR
Read variables from ventilation system
-w WRITE_VAR, --write WRITE_VAR
Write variable to ventilation system
-v VALUE, --value VALUE
Value to write (required with option -v)
-d, --debug Prints debug statements.
Without arguments all readable values using default tty will be retrieved.

Conflict using both memory_profiler and docopt

I have a Python program that uses docopt and appears to parse command-line arguments just fine. However, when I attempt to include memory_profiler in the invocation (with -m memory_profiler), docopt fails to parse the command and prints the usage statement. Here is a sample program:
"""
Usage:
ids_transform [options]
ids_transform --block BLOCK_MSG
ids_transform --unblock
ids_transform --version
Examples:
ids_transform.py --block '2013-03-15 IM#123456 database down'
ids_transform.py -c ../shared/etc/ids_transform.yaml
Options:
-h --help show this help message and exit
-c CONFIG --config=CONFIG config file
-d --debug begin debugging
--force override block file; force run
--profile use cProfile to gather statistics on process
"""
from docopt import docopt
if __name__ == '__main__':
arguments = docopt(__doc__, version='1.0.0rc2')
print(arguments)
Here is a successful invocation:
$ python ids.py -d --force -c foo.yml
{'--block': False,
'--config': 'foo.yml',
'--debug': True,
'--force': True,
'--help': False,
'--profile': False,
'--unblock': False,
'--version': False,
'BLOCK_MSG': None}
And here is the error when utilizing the memory_profiler:
$ python -m memory_profiler ids.py -d --force -c foo.yml
Usage:
ids_transform [options]
ids_transform --block BLOCK_MSG
ids_transform --unblock
ids_transform --version
What am I missing?
It seems memory_profiler doesn't strip itself from sys.argv, so a hack would (I guess) be to do it yourself:
if sys.argv[0].endswith('memory_profiler.py'):
del sys.argv[0]

Categories