AlexeySanko / pybuilder_pytest

Pytest plugin for PyBuilder: https://github.com/pybuilder/pybuilder
6 stars 3 forks source link

Code coverage does not include import statements etc. #11

Closed Anmol-Porwal18 closed 6 years ago

Anmol-Porwal18 commented 6 years ago

following are not included in code coverage:

  1. method names eg. "def pytest"
  2. class names
  3. import statements
AlexeySanko commented 6 years ago

As I understand coverage starts after all import. https://stackoverflow.com/questions/8636828/does-coverage-py-measure-the-function-and-class-definitions I'll try to fix it.

Anmol-Porwal18 commented 6 years ago

okay, thanks

Anmol-Porwal18 commented 6 years ago

@AlexeySanko also when running tests in parallel with "-n m" argument where: m is the number of cores to run tests on

I added the following in "build.py": project.get_property("pytest_extra_args").append("-n 4")

The coverage report warns: Coverage.py warning: No data was collected. (no-data-collected)

and shows the coverage as 0%.

Running without "-n m" argument, it shows the correct coverage.

AlexeySanko commented 6 years ago

@Anmol-Porwal18 For avoiding problems with loaded modules coverage plugin provides parameter project.set_property('coverage_reset_modules', True) Unfortunately, this plugin has problem with pytest module. These problems were resolved into pull request for pytest core module, but this pull request still wasn't merged (pybuilder has global problem with Python 2.6).

In common I strongly recommend to use pytest-cov pytest plugin if You need something more than simple testing.

For example:

project.build_depends_on('pytest-cov')
project.get_property("pytest_extra_args").append("--cov=test_pybuilder")
project.get_property("pytest_extra_args").append("--cov-report=term-missing")

will return

[INFO]  pytest: Run unittests.
============================= test session starts ==============================
platform linux2 -- Python 2.7.3, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 -- /home/alexeysanko/venv_pyb_myproject_pytestplug/bin/python2.7
cachedir: .cache
rootdir: /media/sf_Ubuntu-01/dev/pybuilder_myproject, inifile:
plugins: cov-2.5.1
collecting ... collected 1 item

src/unittest/python/test_helloworld.py::test_helloworld Hello world of Python
PASSED

 generated xml file: /media/sf_Ubuntu-01/dev/pybuilder_myproject/target/reports/junit.xml 

---------- coverage: platform linux2, python 2.7.3-final-0 -----------
Name                                                       Stmts   Miss  Cover   Missing
----------------------------------------------------------------------------------------
src/main/python/test_pybuilder/__init__.py                     1      0   100%
src/main/python/test_pybuilder/helloworld.py                   7      2    71%   11-12
src/main/python/test_pybuilder/read_properties_plugin.py      38     38     0%   19-82
----------------------------------------------------------------------------------------
TOTAL                                                         46     40    13%

=========================== 1 passed in 0.28 seconds ===========================
[INFO]  pytest: All unittests passed.

If You need to pass result to file You can use project.get_property("pytest_extra_args").append("--cov-report=xml:target/reports/pytest_coverage.xml")

AlexeySanko commented 6 years ago

More details about pytest-cov pytest plugin into corresponding repository.

AlexeySanko commented 6 years ago

PS If You use pytest-covpytest plugin do not forget to disable PyBuilder coverage plugin:

# use_plugin('python.coverage')

Otherwise You can get unexpected coverage result or exception.

Anmol-Porwal18 commented 6 years ago

@AlexeySanko yes i am already using pytest-cov plugin. Thanks for the information about pull request on pybuilder and the explanation above.

AlexeySanko commented 6 years ago

Created separated plugin https://github.com/AlexeySanko/pybuilder_pytest/issues/14