Skip to content

Commit

Permalink
Merge 7c4a67c into 507f387
Browse files Browse the repository at this point in the history
  • Loading branch information
brymut committed Jan 14, 2021
2 parents 507f387 + 7c4a67c commit 4db3978
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 95 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/testing.yml
@@ -0,0 +1,50 @@
name: testing

on:
pull_request:
branches:
- '*'
push:
branches:
- master

jobs:
static_analysis:
name: ${{ matrix.make-command }} / Py${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [3.7]
make-command: [doc8, flake8, pylint, test]

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}

- name: Install Python dependencies
run: |
sudo apt-get install virtualenv
pip install -r devel.txt
- name: Prepare for integration test
if: matrix.make-command == 'test'
run: |
echo "[tcms]" > ~/.tcms.conf
echo "url = https://tcms.kiwitcms.org/xml-rpc/" >> ~/.tcms.conf
echo "username = kiwitcms-bot" >> ~/.tcms.conf
echo "password = ${{ secrets.TCMS_PASSWORD }}" >> ~/.tcms.conf
sudo cp .ssl/*.pem /usr/lib/ssl/certs/
sudo update-ca-certificates --fresh
- name: Verify ${{ matrix.make-command }}
run: |
export TCMS_PRODUCT=$GITHUB_REPOSITORY
# branch name or pull/123
export TCMS_PRODUCT_VERSION=$(echo $GITHUB_REF | sed "s|refs/heads/||" | sed "s|refs/||" | sed "s|/merge||")
# short commit number
export TCMS_BUILD=$(echo $GITHUB_SHA | cut -c1-7)
make ${{ matrix.make-command }}
15 changes: 15 additions & 0 deletions Makefile
@@ -0,0 +1,15 @@
.PHONY: doc8
doc8:
doc8 README.rst

.PHONY: flake8
flake8:
@flake8 --exclude=.git *.py tcms_pytest_plugin tests

.PHONY: pylint
pylint:
pylint -d missing-docstring tcms_pytest_plugin/ tests/

.PHONY: test
test:
pytest --kiwitcms
6 changes: 4 additions & 2 deletions README.rst
Expand Up @@ -69,13 +69,15 @@ the coverage at least stays the same before you submit a pull request.
License
-------

Distributed under the terms of the `GNU GPL v3.0`_ license, "kiwitcms-pytest-plugin" is free and open source software
Distributed under the terms of the `GNU GPL v3.0`_ license,
"kiwitcms-pytest-plugin" is free and open source software


Issues
------

If you encounter any problems, please `file an issue`_ along with a detailed description.
If you encounter any problems,
please `file an issue`_ along with a detailed description.

.. _`GNU GPL v3.0`: http://www.gnu.org/licenses/gpl-3.0.txt
.. _`file an issue`: https://github.com/kiwitcms/pytest-plugin/issues
Expand Down
5 changes: 5 additions & 0 deletions devel.txt
@@ -0,0 +1,5 @@
-r requirements.txt

doc8
flake8
pylint_django
74 changes: 37 additions & 37 deletions tcms_pytest_plugin/plugin.py
@@ -1,14 +1,16 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Dmitry Dygalo <dadygalo@gmail.com>
# Licensed under the GPLv3: https://www.gnu.org/licenses/gpl.html
import configparser
import os
# pylint: disable=unused-argument, no-self-use


import attr
import pytest
from tcms_api.plugin_helpers import Backend

DEFAULT_CONFIG_PATH = "~/.tcms.conf"

backend = Backend(prefix='[pytest]')


def pytest_addoption(parser):
group = parser.getgroup("kiwitcms")
Expand All @@ -19,42 +21,40 @@ def pytest_addoption(parser):

def pytest_configure(config):
if config.getoption("--kiwitcms"):
config_manager = ConfigManager(config)
config.pluginmanager.register(
Plugin(
api_url=config_manager.get_option("url", "TCMS_API_URL"),
username=config_manager.get_option("username", "TCMS_USERNAME"),
password=config_manager.get_option("password", "TCMS_PASSWORD"),
),
KiwiTCMSPlugin(),
name="pytest-kiwitcms",
)


def is_not_none(instance, attribute, value): # pylint: disable=unused-argument
if not value:
pytest.exit(msg=f"Option {attribute.name} is empty", returncode=1)


@attr.s(hash=True)
class Plugin: # pylint: disable=too-few-public-methods
api_url = attr.ib(type=str, validator=is_not_none)
username = attr.ib(type=str, validator=is_not_none)
password = attr.ib(type=str, validator=is_not_none)


@attr.s
class ConfigManager: # pylint: disable=too-few-public-methods
"""Helper for configuration parsing."""

pytest_config = attr.ib()
config_file = attr.ib(init=False)

def __attrs_post_init__(self):
self.config_file = configparser.ConfigParser()
self.config_file.read(os.path.expanduser(DEFAULT_CONFIG_PATH))

def get_option(self, config_name, envvar):
try:
return self.config_file["tcms"][config_name]
except KeyError:
return os.getenv(envvar)
class KiwiTCMSPlugin:
test_execution_id = 0
status_id = 0
comment = ''

def pytest_runtestloop(self, session):
backend.configure()

def pytest_runtest_logstart(self, nodeid, location):
test_case, _ = backend.test_case_get_or_create(nodeid)
backend.add_test_case_to_plan(test_case['id'], backend.plan_id)
self.test_execution_id = backend.add_test_case_to_run(test_case['id'], backend.run_id)

@pytest.hookimpl(hookwrapper=True)
def pytest_report_teststatus(self, report, config):
yield
if report.when == 'teardown':
if report.outcome == 'passed':
self.status_id = backend.get_status_id('PASSED')
elif report.outcome == 'failed':
self.status_id = backend.get_status_id('FAILED')
elif report.outcome == 'skipped':
self.status_id = backend.get_status_id('WAIVED')

def pytest_runtest_logfinish(self, nodeid, location):
backend.update_test_execution(self.test_execution_id, self.status_id, self.comment)

@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(self, terminalreporter, exitstatus, config):
yield
backend.finish_test_run()
2 changes: 1 addition & 1 deletion tests/conftest.py
Expand Up @@ -2,7 +2,7 @@
# Licensed under the GPLv3: https://www.gnu.org/licenses/gpl.html
import pytest

pytest_plugins = "pytester" # pylint: disable=invalid-name
pytest_plugins = ['pytester'] # pylint: disable=invalid-name


DEFAULT_CONFIG_CONTENT = """
Expand Down
127 changes: 72 additions & 55 deletions tests/test_pytest_kiwitcms.py
Expand Up @@ -11,68 +11,85 @@
)


@pytest.mark.usefixtures("config_file")
def test_file_configuration(testdir):
"""Load configuration from ~/.tcms.conf."""
testdir.makepyfile(
"""
def test_file_configuration(pytestconfig):
plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
assert plugin.api_url == "http://example.com"
assert plugin.username == "admin"
assert plugin.password == "secret"
"""
)
result = testdir.runpytest("--kiwitcms", "-v")
result.stdout.fnmatch_lines(["*::test_file_configuration PASSED*"])
assert result.ret == 0
# @pytest.mark.usefixtures("config_file")
# def test_file_configuration(testdir):
# """Load configuration from ~/.tcms.conf."""
# testdir.makepyfile(
# """
# def test_file_configuration(pytestconfig):
# plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
# assert plugin.api_url == "http://example.com"
# assert plugin.username == "admin"
# assert plugin.password == "secret"
# """
# )
# result = testdir.runpytest("--kiwitcms", "-v")
# result.stdout.fnmatch_lines(["*::test_file_configuration PASSED*"])
# assert result.ret == 0


def test_env_configuration(testdir, monkeypatch):
"""If ~/.tcms.conf doesn't exist - use env variables."""
for envvar, value in DEFAULT_CONFIG:
monkeypatch.setenv(envvar, value)
testdir.makepyfile(
"""
def test_env_configuration(pytestconfig):
plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
assert plugin.api_url == "http://another.com"
assert plugin.username == "user"
assert plugin.password == "random"
"""
)
result = testdir.runpytest("--kiwitcms", "-v")
result.stdout.fnmatch_lines(["*::test_env_configuration PASSED*"])
assert result.ret == 0
# def test_env_configuration(testdir, monkeypatch):
# """If ~/.tcms.conf doesn't exist - use env variables."""
# for envvar, value in DEFAULT_CONFIG:
# monkeypatch.setenv(envvar, value)
# testdir.makepyfile(
# """
# def test_env_configuration(pytestconfig):
# plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
# assert plugin.api_url == "http://another.com"
# assert plugin.username == "user"
# assert plugin.password == "random"
# """
# )
# result = testdir.runpytest("--kiwitcms", "-v")
# result.stdout.fnmatch_lines(["*::test_env_configuration PASSED*"])
# assert result.ret == 0


@pytest.mark.parametrize("envvar, value", DEFAULT_CONFIG)
@pytest.mark.usefixtures("config_file")
def test_config_file_precedence(testdir, monkeypatch, envvar, value):
"""Values from ~/.tcms.conf have higher priority than env variables."""
monkeypatch.setenv(envvar, value)
testdir.makepyfile(
"""
def test_config_file_precedence(pytestconfig):
plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
assert plugin.api_url == "http://example.com"
assert plugin.username == "admin"
assert plugin.password == "secret"
"""
)
result = testdir.runpytest("--kiwitcms", "-v")
result.stdout.fnmatch_lines(["*::test_config_file_precedence PASSED*"])
assert result.ret == 0
# @pytest.mark.parametrize("envvar, value", DEFAULT_CONFIG)
# @pytest.mark.usefixtures("config_file")
# def test_config_file_precedence(testdir, monkeypatch, envvar, value):
# """Values from ~/.tcms.conf have higher priority than env variables."""
# monkeypatch.setenv(envvar, value)
# testdir.makepyfile(
# """
# def test_config_file_precedence(pytestconfig):
# plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
# assert plugin.api_url == "http://example.com"
# assert plugin.username == "admin"
# assert plugin.password == "secret"
# """
# )
# result = testdir.runpytest("--kiwitcms", "-v")
# result.stdout.fnmatch_lines(["*::test_config_file_precedence PASSED*"])
# assert result.ret == 0


# def test_empty_variable(testdir):
# """Any empty value in config variables will lead to program exiting."""
# testdir.makepyfile(
# """
# def test_empty_variable(pytestconfig):
# plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
# """
# )
# result = testdir.runpytest("--kiwitcms", "-v")
# result.stderr.fnmatch_lines(["Exit: Option api_url is empty"])
# assert result.ret == 1

def test_empty_variable(testdir):
"""Any empty value in config variables will lead to program exiting."""
def test_kiwitcms_pytest(testdir):
testdir.makepyfile(
"""
def test_empty_variable(pytestconfig):
plugin = pytestconfig.pluginmanager.get_plugin("pytest-kiwitcms")
import pytest
def test_pass():
assert 1 == 1
def test_fail():
assert 1 == 2
@pytest.mark.skip()
def test_skip():
assert 1 == 1
def test_error(test):
assert 1 == ""
"""
)
result = testdir.runpytest("--kiwitcms", "-v")
result.stderr.fnmatch_lines(["Exit: Option api_url is empty"])
assert result.ret == 1
testdir.runpytest("--kiwitcms")

0 comments on commit 4db3978

Please sign in to comment.