Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test][NFC] Change from nose to pytest (web library) #3932

Merged
merged 5 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scripts/test/func_template/template__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# coding=utf-8
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------
169 changes: 169 additions & 0 deletions scripts/test/func_template/template_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------

""" $TEST_NAME$ function test.

WARNING!!!
This is a generated test skeleton remove the parts not required by the tests.
WARNING!!!

"""


import os
import shutil
import unittest
import uuid

from libtest import codechecker
from libtest import env
from libtest import project


class TestSkeleton(unittest.TestCase):

_ccClient = None

def setup_class(self):
"""Setup the environment for testing $TEST_NAME$."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('$TEST_NAME$')

# Set the TEST_WORKSPACE used by the tests.
os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE

# Get the clang version which is used for testing.
# Important because different clang releases might
# find different errors.
clang_version = env.clang_to_test()

test_config = {}

test_project = 'cpp'

project_info = project.get_info(test_project)

# Copy the test project to the workspace. The tests should
# work only on this test project.
test_proj_path = os.path.join(TEST_WORKSPACE, "test_proj")
shutil.copytree(project.path(test_project), test_proj_path)

project_info['project_path'] = test_proj_path

# Generate a unique name for this test run.
test_project_name = project_info['name'] + '_' + uuid.uuid4().hex

test_config['test_project'] = project_info

# Suppress file should be set here if needed by the tests.
suppress_file = None

# Skip list file should be set here if needed by the tests.
skip_list_file = None

# Get an environment which should be used by the tests.
test_env = env.test_env(TEST_WORKSPACE)

# Create a basic CodeChecker config for the tests, this should
# be imported by the tests and they should only depend on these
# configuration options.
codechecker_cfg = {
'suppress_file': suppress_file,
'skip_list_file': skip_list_file,
'check_env': test_env,
'workspace': TEST_WORKSPACE,
'checkers': [],
'analyzers': ['clangsa', 'clang-tidy']
}

# Start or connect to the running CodeChecker server and get connection
# details.
print("This test uses a CodeChecker server... connecting...")
server_access = codechecker.start_or_get_server()
server_access['viewer_product'] = '$TEST_NAME$'
codechecker.add_test_package_product(server_access, TEST_WORKSPACE)

# Extend the checker configuration with the server access.
codechecker_cfg.update(server_access)

# Clean the test project, if needed by the tests.
ret = project.clean(test_project)
if ret:
sys.exit(ret)

# Check the test project, if needed by the tests.
ret = codechecker.check_and_store(codechecker_cfg,
test_project_name,
project.path(test_project))
if ret:
sys.exit(1)
print("Analyzing the test project was successful.")

# Save the run names in the configuration.
codechecker_cfg['run_names'] = [test_project_name]

test_config['codechecker_cfg'] = codechecker_cfg

# Export the test configuration to the workspace.
env.export_test_cfg(TEST_WORKSPACE, test_config)


def teardown_class(self):
"""Clean up after the test."""

# TODO: If environment variable is set keep the workspace
# and print out the path.
global TEST_WORKSPACE

check_env = env.import_test_cfg(TEST_WORKSPACE)[
'codechecker_cfg']['check_env']
codechecker.remove_test_package_product(TEST_WORKSPACE, check_env)

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE, ignore_errors=True)

def setup_method(self, method):
"""
WARNING!!!
This is an example how to get the configurations needed by the tests.
WARNING!!!
"""

# TEST_WORKSPACE is automatically set by test package __init__.py .
test_workspace = os.environ['TEST_WORKSPACE']

test_class = self.__class__.__name__
print('Running ' + test_class + ' tests in ' + test_workspace)

# Get the test configuration from the prepared int the test workspace.
test_cfg = env.import_test_cfg(test_workspace)

# Get the test project configuration from the prepared test workspace.
self._testproject_data = env.setup_test_proj_cfg(test_workspace)
self.assertIsNotNone(self._testproject_data)

# Setup a viewer client to test viewer API calls.
self._cc_client = env.setup_viewer_client(test_workspace)
self.assertIsNotNone(self._cc_client)

# Get the CodeChecker cmd if needed for the tests.
self._codechecker_cmd = env.codechecker_cmd()

# Get the run names which belong to this test.
run_names = env.get_run_names(test_workspace)

runs = self._cc_client.getRunData(None, None, 0, None)
test_runs = [run for run in runs if run.name in run_names]

def test_skel(self):
"""
Test some feature.
"""
pass
13 changes: 0 additions & 13 deletions web/.noserc

This file was deleted.

4 changes: 2 additions & 2 deletions web/client/tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CLIENT_UNIT_TEST_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) nosetests $(NOSECFG) -w client tests/unit
CLIENT_UNIT_TEST_COV_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) coverage run -m nose $(NOSECFG) -w client tests/unit && coverage report && coverage html
CLIENT_UNIT_TEST_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) pytest $(PYTESTCFG) client client/tests/unit
CLIENT_UNIT_TEST_COV_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) coverage run -m pytest $(PYTESTCFG) client client/tests/unit && coverage report && coverage html


test_unit_client:
Expand Down
8 changes: 4 additions & 4 deletions web/client/tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
Setup python modules for the unit tests.
"""


import os
import sys

# Add the generated thrift files for the unit tests.
BUILD_DIR = os.path.abspath(os.environ['BUILD_DIR'])

REPO_ROOT = os.path.abspath(os.environ['REPO_ROOT'])
PKG_ROOT = os.path.join(REPO_ROOT, 'build', 'CodeChecker')

os.environ["CC_DATA_FILES_DIR"] = PKG_ROOT

sys.path.append(REPO_ROOT)
sys.path.append(os.path.join(REPO_ROOT, 'web'))
sys.path.append(os.path.join(REPO_ROOT, 'tools', 'report-converter'))
sys.path.append(os.path.join(PKG_ROOT, 'lib', 'python3'))
13 changes: 13 additions & 0 deletions web/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[pytest]

addopts =
# increase verbosity level
--verbose

# stop running tests on first error
# FIXME: Pretty please comment this horrible setting out, I hate it with a
# passion
# --maxfail=1

# do not capture stdout
--capture=fd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In analyzer tests this option was --capture=sys. Shouldn't they match?

2 changes: 1 addition & 1 deletion web/requirements_py/dev/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pg8000==1.15.2
psutil==5.8.0
portalocker==2.2.1
pylint==2.8.2
nose==1.3.7
pytest==7.3.1
mkdocs==1.2.3
mypy_extensions==0.4.3
coverage==5.5.0
Expand Down
4 changes: 2 additions & 2 deletions web/server/tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SERVER_UNIT_TEST_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) nosetests $(NOSECFG) -w server tests/unit
SERVER_UNIT_TEST_COV_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) coverage run -m nose $(NOSECFG) -w server tests/unit && coverage report && coverage html
SERVER_UNIT_TEST_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) pytest $(PYTESTCFG) server server/tests/unit
SERVER_UNIT_TEST_COV_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) coverage run -m pytest $(PYTESTCFG) server server/tests/unit && coverage report && coverage html

test_unit_server:
$(SERVER_UNIT_TEST_CMD)
Expand Down
8 changes: 4 additions & 4 deletions web/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ WOKSPACE_GLOBAL_SIMPLE_SERVER = $(CC_TEST_WORKSPACE_ROOT)/global_simple_server

CLEAR_WORKSPACE_CMD = rm -rf $(CC_TEST_WORKSPACE_ROOT)

# Nose test runner configuration options.
NOSECFG = --config .noserc
# pytest test runner configuration options.
PYTESTCFG = -c pytest.ini

test: pycodestyle pylint test_unit test_functional

Expand Down Expand Up @@ -75,7 +75,7 @@ SHUTDOWN_GLOBAL_SERVERS_CMD = \
EXIT_ERROR = { ${SHUTDOWN_GLOBAL_SERVERS_CMD}; exit 1; }

FUNCTIONAL_TEST_CMD = $(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) $(CLANG_VERSION) $(TEST_PROJECT) \
nosetests $(NOSECFG) tests/functional \
pytest $(PYTESTCFG) tests/functional \
&& { ${SHUTDOWN_GLOBAL_SERVERS_CMD}; } || ${EXIT_ERROR}

MAKE_DB_CMD = bash -c 'psql -h localhost \
Expand All @@ -88,7 +88,7 @@ DROP_DB_CMD = bash -c 'psql -h localhost \

RUN_TEST_CMD = $(CLEAR_WORKSPACE_CMD) && \
$(REPO_ROOT) BUILD_DIR=$(BUILD_DIR) $(CLANG_VERSION) $(TEST_PROJECT) \
nosetests $(NOSECFG) $(ROOT)/web/${TEST} \
pytest $(PYTESTCFG) $(ROOT)/web/${TEST} \
&& { ${SHUTDOWN_GLOBAL_SERVERS_CMD}; } || ${EXIT_ERROR}

run_test:
Expand Down
5 changes: 3 additions & 2 deletions web/tests/functional/authentication/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
TEST_WORKSPACE = None


def setup_package():
def setup_class_common():
"""Setup the environment for the tests then start the server."""

global TEST_WORKSPACE
Expand Down Expand Up @@ -65,7 +65,7 @@ def setup_package():
codechecker.add_test_package_product(host_port_cfg, TEST_WORKSPACE)


def teardown_package():
def teardown_class_common():
"""Stop the CodeChecker server and clean up after the tests."""
# TODO If environment variable is set keep the workspace
# and print out the path.
Expand All @@ -77,6 +77,7 @@ def teardown_package():
codechecker_cfg['check_env'])

__STOP_SERVER.set()
__STOP_SERVER.clear()

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE, ignore_errors=True)
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
from libtest import codechecker
from libtest import env

from . import setup_class_common, teardown_class_common


class DictAuth(unittest.TestCase):
"""
Dictionary based authentication tests.
"""

def setUp(self):
def setup_class(self):
setup_class_common()

def teardown_class(self):
teardown_class_common()

def setup_method(self, method):
# Get the test workspace used to authentication tests.
self._test_workspace = os.environ['TEST_WORKSPACE']

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@

from libtest import codechecker, env

from . import setup_class_common, teardown_class_common


class PermissionManagement(unittest.TestCase):

def setUp(self):
def setup_class(self):
setup_class_common()

def teardown_class(self):
teardown_class_common()

def setup_method(self, method):
# TEST_WORKSPACE is automatically set by test package __init__.py .
self._test_workspace = os.environ['TEST_WORKSPACE']

Expand Down
Loading