forked from pybuilder/pybuilder
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pytest_plugin pybuilder#13(pybuilder#448)
- Loading branch information
AlexeySanko
committed
Oct 15, 2017
1 parent
d0f8f80
commit fed57de
Showing
9 changed files
with
361 additions
and
39 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/integrationtest/python/should_correctly_pass_failure_pytest_plugin_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of PyBuilder | ||
# | ||
# Copyright 2011-2015 PyBuilder Team | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from os import sep as os_sep | ||
import unittest | ||
|
||
from integrationtest_support import IntegrationTestSupport | ||
from pybuilder.errors import BuildFailedException | ||
|
||
|
||
failure_pytest_test_content = """ | ||
def test_pytest_base_failure(): | ||
assert False | ||
""" | ||
|
||
|
||
class TestDefaultParamFailure(IntegrationTestSupport): | ||
def test_pytest(self): | ||
self.write_build_file(""" | ||
from pybuilder.core import use_plugin, init | ||
use_plugin("python.pytest") | ||
@init | ||
def init (project): | ||
pass | ||
""") | ||
|
||
unittest_path = "src/unittest/python" | ||
unittest_file = unittest_path + os_sep + "failure_test.py" | ||
report_file = "target/reports/junit.xml" | ||
|
||
self.create_directory(unittest_path) | ||
self.write_file(unittest_file, failure_pytest_test_content) | ||
|
||
reactor = self.prepare_reactor() | ||
self.assertRaises(BuildFailedException, reactor.build, "run_unit_tests") | ||
|
||
self.assert_file_exists(unittest_file) | ||
self.assert_file_exists(report_file) | ||
|
||
self.assert_file_contains(report_file, 'tests="1"') | ||
self.assert_file_contains(report_file, 'errors="0"') | ||
self.assert_file_contains(report_file, 'failures="1"') | ||
self.assert_file_contains(report_file, 'skips="0"') | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
69 changes: 69 additions & 0 deletions
69
src/integrationtest/python/should_run_pytest_plugin_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of PyBuilder | ||
# | ||
# Copyright 2011-2015 PyBuilder Team | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from os import sep as os_sep | ||
import unittest | ||
|
||
from integrationtest_support import IntegrationTestSupport | ||
|
||
|
||
success_pytest_test_content = """ | ||
def test_pytest_base_success(): | ||
assert True | ||
def test_pytest_base_skip(): | ||
assert True | ||
""" | ||
|
||
|
||
class TestCustomParamSuccess(IntegrationTestSupport): | ||
def test_pytest(self): | ||
self.write_build_file(""" | ||
from pybuilder.core import use_plugin, init | ||
use_plugin("python.pytest") | ||
@init | ||
def init (project): | ||
project.set_property("dir_source_pytest_python", "some_dir/unittest") | ||
project.set_property("pytest_report_file", "some_dir/junit.xml") | ||
project.get_property("pytest_extra_args").append("-k") | ||
project.get_property("pytest_extra_args").append("test_pytest_base_success") | ||
""") | ||
|
||
unittest_path = "some_dir/unittest" | ||
unittest_file = unittest_path + os_sep + "success_test.py" | ||
report_file = "some_dir/junit.xml" | ||
|
||
self.create_directory(unittest_path) | ||
self.write_file(unittest_file, success_pytest_test_content) | ||
|
||
reactor = self.prepare_reactor() | ||
reactor.build("run_unit_tests") | ||
|
||
self.assert_file_exists(unittest_file) | ||
self.assert_file_exists(report_file) | ||
|
||
self.assert_file_contains(report_file, 'tests="1"') | ||
self.assert_file_contains(report_file, 'errors="0"') | ||
self.assert_file_contains(report_file, 'failures="0"') | ||
self.assert_file_contains(report_file, 'skips="0"') | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2016 Alexey Sanko | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from sys import path as sys_path | ||
|
||
from pybuilder.core import task, init, use_plugin, after | ||
from pybuilder.errors import MissingPrerequisiteException, BuildFailedException | ||
from pybuilder.utils import register_test_and_source_path_and_return_test_dir | ||
|
||
__author__ = 'Alexey Sanko' | ||
|
||
use_plugin("python.core") | ||
|
||
|
||
@init | ||
def initialize_pytest_plugin(project): | ||
""" Init default plugin project properties. """ | ||
project.plugin_depends_on('pytest') | ||
project.set_property_if_unset("dir_source_pytest_python", "src/unittest/python") | ||
project.set_property_if_unset("pytest_report_file", "target/reports/junit.xml") | ||
project.set_property_if_unset("pytest_extra_args", []) | ||
|
||
|
||
@after("prepare") | ||
def assert_pytest_available(logger): | ||
""" Asserts that the pytest module is available. """ | ||
logger.debug("Checking if pytest module is available.") | ||
|
||
try: | ||
import pytest | ||
logger.debug("Found pytest version %s" % pytest.__version__) | ||
except ImportError: | ||
raise MissingPrerequisiteException(prerequisite="pytest module", caller="plugin python.pytest") | ||
|
||
|
||
@task | ||
def run_unit_tests(project, logger): | ||
""" Call pytest for the sources of the given project. """ | ||
logger.info('pytest: Run unittests.') | ||
from pytest import main as pytest_main | ||
test_dir = register_test_and_source_path_and_return_test_dir(project, sys_path, 'pytest') | ||
extra_args = project.get_property("pytest_extra_args") | ||
try: | ||
pytest_args = [test_dir] | ||
if project.get_property('verbose'): | ||
pytest_args.append('-s') | ||
pytest_args.append('-v') | ||
pytest_args.append('--junit-xml') | ||
pytest_args.append(project.expand_path('$pytest_report_file')) | ||
pytest_args = pytest_args + (extra_args if extra_args else []) | ||
ret = pytest_main(pytest_args) | ||
if ret: | ||
raise BuildFailedException('pytest: unittests failed') | ||
else: | ||
logger.info('pytest: All unittests passed.') | ||
except: | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/unittest/python/plugins/python/pytest_plugin_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of PyBuilder | ||
# | ||
# Copyright 2011-2015 PyBuilder Team | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from unittest import TestCase | ||
|
||
from pybuilder.core import Project | ||
from pybuilder.plugins.python.pytest_plugin import initialize_pytest_plugin | ||
from test_utils import Mock | ||
|
||
|
||
class PytestPluginInitializationTests(TestCase): | ||
def setUp(self): | ||
self.project = Project("basedir") | ||
|
||
def test_should_set_dependency(self): | ||
mock_project = Mock(Project) | ||
initialize_pytest_plugin(mock_project) | ||
mock_project.plugin_depends_on.assert_called_with('pytest') | ||
|
||
def test_should_set_default_values_when_initializing_plugin(self): | ||
initialize_pytest_plugin(self.project) | ||
|
||
self.assertEquals( | ||
self.project.get_property("dir_source_pytest_python"), "src/unittest/python") | ||
self.assertEquals( | ||
self.project.get_property("pytest_report_file"), "target/reports/junit.xml") | ||
self.assertEquals( | ||
self.project.get_property("pytest_extra_args"), []) | ||
|
||
def test_should_leave_user_specified_properties_when_initializing_plugin(self): | ||
|
||
expected_properties = { | ||
"dir_source_pytest_python": "source_dir", | ||
"pytest_report_file": "output_dir/report_file", | ||
"pytest_extra_args": ['-a'] | ||
} | ||
|
||
for property_name, property_value in expected_properties.items(): | ||
self.project.set_property(property_name, property_value) | ||
|
||
initialize_pytest_plugin(self.project) | ||
|
||
for property_name, property_value in expected_properties.items(): | ||
self.assertEquals( | ||
self.project.get_property(property_name), | ||
property_value) | ||
|
||
|
||
# PyByuilder build dependency on pytest was removed by PyBuilder developers' request: | ||
# https://github.com/pybuilder/pybuilder/pull/448#discussion_r93954475 | ||
# So we need to disable next tests and decrease % coverage. | ||
# This functionality is covered by next integreation test: | ||
# should_correctly_pass_failure_pytest_plugin_tests | ||
# should_run_pytest_plugin_tests | ||
# | ||
# class PytestPluginAssertAvailableTests(TestCase): | ||
# def setUp(self): | ||
# self.project = Project("basedir") | ||
# | ||
# @patch('pytest.__version__') | ||
# def test_should_assert_available(self, pytest_version): | ||
# logger_mock = Mock() | ||
# assert_pytest_available(logger_mock) | ||
# self.assertEqual(logger_mock.debug.call_count, 2) | ||
# | ||
# | ||
# class PytestPluginRunTests(TestCase): | ||
# def setUp(self): | ||
# self.project = Project("basedir") | ||
# self.project.set_property('verbose', True) | ||
# self.project.set_property('dir_source_main_python', 'src') | ||
# initialize_pytest_plugin(self.project) | ||
# | ||
# @patch('pytest.main', return_value=None) | ||
# def test_should_call_pytest_main_with_arguments(self, main): | ||
# run_unit_tests(self.project, Mock()) | ||
# main.assert_called_with(['basedir/src/unittest/python', '-s', '-v', '--junit-xml', | ||
# 'basedir/target/reports/junit.xml']) | ||
# | ||
# @patch('pytest.main', return_value=1) | ||
# def test_should_raise_if_pytest_return_code(self, main): | ||
# self.assertRaises( | ||
# BuildFailedException, | ||
# run_unit_tests, | ||
# self.project, | ||
# Mock() | ||
# ) |
Oops, something went wrong.