Buildout + Nose failing with passed options options - python

After running a buildout operation on my project, I can run nose with the following command:
# ./bin/nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.310s
However, when I try to pass options (such as -w for the base directory, I get the following:
# ./bin/nosetests -vv --detailed-errors --exe
Usage: nosetests [options]
nosetests: error: no such option: -v
I've checked the test files which are being ran, and removed all lines importing either getopt or OptionParser to ensure they're not getting in the way, but I'm still getting the same error regardless.
I believe one of the files we're testing requires getopt to function... is there any way I can get nosetests to work with buildout without these errors?

You can use noserunner buildout recipe
Here is example buildout.cfg:
[buildout]
parts = test
index = http://download.zope.org/simple
[test]
recipe = pbp.recipe.noserunner
eggs = pbp.recipe.noserunner
working-directory = ${buildout:directory}
This will create script test in bin directory. Runner will run all tests found in path set in working-directory

Related

No tests collected by pytest

I have problem in importing pytest while writing a python code. "import pytest is grayed out.
Python is 3.8.3, Pycharm community edition.
pytest version 5.4.2, is successfully installed and can be seen in the project interpreter in pycharm. As well as I can see the installed path of pytest in python directory.
When running py.test command from console. It starts the test run shows "collected 0 items" and lastly ends with "NO TESTS RAN IN 0.05s"
If anyone running similar problems with some other packages kindly let me know.
TIA...
You simply run pytest from the commandline. There is no need to import pytest into a script. Take this Python script as an example:
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 4
To run pytest on it, from the terminal (after changing to the right directory):
$ pytest
And you will then see the test outcome in the commandline as pytest automatically picks up the python scripts names test_*.py, where * is any name, e.g. test_increment.py. To have a test from your Python script run, name it with test_ as well to begin with.
Running pytest in the terminal is an option. In addition, Pycharm has integrated test suite for automatic discovery and collection of test tasks. You can use hotkey ctrl+shift+10 to run the test tasks directly in current file .

Pylint with sniffer not using updated source files

I am using Pylint and Nose along with sniffer to lint and test my python application on every save. This is sniffer in case you are unaware https://pypi.python.org/pypi/sniffer
Here is the runnable responsible for running nosetests and pylint from scent.py file for sniffer
from sniffer.api import * # import the really small API
from pylint import lint
from subprocess import call
import os
import nose
#runnable
def zimmer_tests(*args):
command = "nosetests some_module/test --nologcapture --with-coverage --cover-config-file=zimmer_coverage_config"
lint.Run(['some_module/app'], exit=False)
return call(command, shell=True) == 0
Here, first lint.Run() runs pylint on my app. Then nosetest is executed on the app using call()
The Problem is that after I save the file nosetests run on updated version of the file however Pylint uses the same old version. I have to restart sniffer every time for pylint to get new version of files.
I assume this is not a problem of sniffer's configuration since nosetests is able to get the new version of file every time. Still I am not sure.
My pylintrc file is almost the same we get from generate command pylint --generate-rcfile > .pylintrc with some minor application specific tweaks.
As pointed by #KlausD. in comments, the lint.Run() was using files from cache as it was being called from a still running process. Calling pylint from command line worked as expected.
Here is the modified code
#runnable
def zimmer_tests(*args):
command_nose = "nosetests some_module/test --nologcapture --with-coverage --cover-config-file=zimmer_coverage_config"
command_lint = "pylint some_module"
call(command_lint, shell=True)
return call(command_nose, shell=True) == 0

Python: How to run tests made with unittest

This is the structure of my project, and I would like to run the test that I have made from the command line.
I am using the following command:
python test_hotel.py
However I am getting the following error
ImportError: No module named 'hotel'
What can I do to solve this problem, and is there a way to execute the whole tests in a project from the command line.
Thanks in advance.
For running unittest from commandline, you should use this command:
python -m unittest tests.test_hotel
You need to make sure that you have followed the rules in writing unittests (https://docs.python.org/2/library/unittest.html)
As #shahram kalantari said to run a tests the command line is:
python -m unittest tests.test_hotel
If one wants to run the whole tests the command line is:
python -m unittest discover tests
If you want more information about what tests were run the -v flag should be included:
python -m unittest discover tests -v

Generating allure report using pytest

I am using py test allure adaptor and trying to generate input data required for allure report. But I am not able to generate any XML's. When I execute the py file using py.test sample.py, it did create pycache dir. Then I executed "allure generate -v 1.3.9 C:\allurereports" (This is the dir where I had the sample.py). It did create an allure html report but no of test cases was 0. No details were present.
The sample.py(it is same as given in the example)
import allure
#allure.feature('Feature1')
#allure.story('Story1')
def test_minor():
assert False
#allure.feature('Feature2')
#allure.story('Story2', 'Story3')
#allure.story('Story4')
class TestBar:
# will have 'Feature2 and Story2 and Story3 and Story4'
def test_bar(self):
pass
Here's the py.test command used:
py.test sample.py --allure_features=feature1,feature2
Can anybody help me how to generate an allure report from the file? What are the commands to execute?
Lavanya.
I'll try to explain the sequence you must to perform to generate allure report of autotest.
Install pip. Download get-pip.py and perform python get-pip.py.
Install pytest and pytest-allure-adaptor via pip. Perform python -m pip install pytest allure-pytest
Generate autotest allure xml report. Perform python -m pytest sample.py --alluredir <some directory>
In <some directory> appear xml autotest report, which contain results of sample.py tests. Let's make beauty html report via an allure-cli tool.
Install allure-cli. Download last version of allure-cli. allure-cli requires java. allure-cli doesn't require installation, just unpack and use it.
Generate html report. Find allure (allure.bat for Windows) in unpacked zip. Perform allure.bat generate -o <some directory> -v 1.4.0 <some directory>
Find index.html in <some directory> and open it via a browser.
*Note <some directory> the same for all steps
There is a very simple way to generate reports via allure:
first, install allure:
allure-pytest 2.6.0
allure-python-commons 2.6.0
Then, if you are unable to generate the reports, follow below steps:
(using pytest)
pytest test_xyz.py --alluredir=path_where_you_want_to_save_reports
allure serve report_path
If it is still showing allure is not recognized command (blah -blah), then install allure using npm plugin with below command:
npm install -g allure-commandline --save-dev
then follow step (2) again, then one server will start and you will be able to see allure reports.
This is something that I found working -
python3 -m pytest [your_test_class_name] --alluredir \Report
Then perform the below line where you saved your report -
python3 -m allure serve \Report
This will open up the allure report in your default browser.
You should specify directory with your test data (the directory which contains -testsuite.xml files), not a test directory.
You can use py.test --alluredir [path_to_report_dir] to specify it.
PS. Make sure you use right version of allure (latest pytest adapter supports only allure 1.4.*).
For more information see https://github.com/allure-framework/allure-python and https://github.com/allure-framework/allure-cli
now you must use allure-command-line instead of allure-cli to generate html-report, cause the second one is deprecated.

Why does nosetests say --with-coverage is not an option?

I am using nosetests to automatically discover and run my unittests. I would also like to have it generate coverage reports.
When I run nosetests with the following command everything works just fine
nosetests .
I looked up online that to generate the coverage, nosetests has a command line argument --with-coverage. I also double checked that this command exists using nosetests --help. However, whenever I run the following command I get the following output
nosetests --with-coverage .
Usage: nosetests [options]
nosetests: error: no such option: --with-coverage
I double checked that the coverage plugin is installed by running
nosetests --plugins
coverage shows up in the list along with a bunch of other plugins.
I also know I have coverage installed because I can manually run the coverage data collection using something along the lines of:
coverage run test.py
Am I misusing the --with-coverage option? Or is there something else I am missing?
Thanks in advance.
I never got the command line options working. I did what Janne Karila suggested and created a setup.cfg file in my projects main directory. Once I had that file I could just run nosetests with no arguments and everything would run.
One problem I had when trying to create my document was that I couldn't figure out what parameters were allowed in the config. But it turns out that any of the commands listed here https://nose.readthedocs.org/en/latest/usage.html#options can be used. Just leave off the double dashes before the command.
For reference my current config file is
[nosetests]
verbosity=1
detailed-errors=1
with-coverage=1
cover-erase=1
cover-package=application
cover-html=1
cover-html-dir=htmlcov
where=tests
This config file says to use coverage, to erase the previous run's coverage, to only report on the files in the application package, and to output an html report to the htmlcov directory.
Hopefully this will help someone else in the future.
Your syntax is correct. It maybe an issue with your environment, double check your python environment and where your have nose and coverage installed. As a sanity check, you can quickly setup a new virtualenv, install nose, and run the command with the coverage option.
As of nose 1.3.7 - the most recent version available on Pypy - that command doesn't exist:
https://github.com/nose-devs/nose/blob/release_1.3.7/nose/plugins/cover.py
It looks like the documentation is being generated from the master branch of the project that does have those options available:
https://github.com/nose-devs/nose/blob/master/nose/plugins/cover.py
What you can do is install nose from the master branch like this:
pip install git+https://github.com/nose-devs/nose#master --upgrade
It will say it just installed 1.3.7 but that's only because the version hasn't been bumped in the project setup.py file yet: https://github.com/nose-devs/nose/blob/master/setup.py#L4
Remember you have just installed an unreleased version of nose, there may be other bugs.

Categories

Resources