Skip to content

Commit

Permalink
Merge pull request Ericsson#3926 from Szelethus/nose_to_pytest
Browse files Browse the repository at this point in the history
[test][NFC] Change from nose to pytest (analyzer library)
  • Loading branch information
bruntib authored Jun 6, 2023
2 parents 187d117 + ba8184d commit c8db700
Show file tree
Hide file tree
Showing 44 changed files with 509 additions and 613 deletions.
13 changes: 0 additions & 13 deletions analyzer/.noserc

This file was deleted.

13 changes: 13 additions & 0 deletions analyzer/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=sys
2 changes: 1 addition & 1 deletion analyzer/requirements_py/dev/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lxml==4.9.2
nose==1.3.7
pytest==7.3.1
pycodestyle==2.7.0
psutil==5.8.0
portalocker==2.2.1
Expand Down
14 changes: 7 additions & 7 deletions analyzer/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ PYTHON_BIN ?= python3

REPO_ROOT ?= REPO_ROOT=$(ROOT)

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

test_in_env: pycodestyle_in_env pylint_in_env test_unit_in_env test_functional_in_env test_build_logger test_tu_collector_in_env

Expand All @@ -33,14 +33,14 @@ pylint_in_env: venv_dev

run_test:
$(REPO_ROOT) $(TEST_PROJECT) \
nosetests $(NOSECFG) ${TEST} || exit 1
pytest $(PYTESTCFG) ${TEST} || exit 1

run_test_in_env: venv_dev
$(ACTIVATE_DEV_VENV) && $(REPO_ROOT) $(TEST_PROJECT) \
nosetests $(NOSECFG) ${TEST} || exit 1
pytest $(PYTESTCFG) ${TEST} || exit 1

UNIT_TEST_CMD = $(REPO_ROOT) nosetests $(NOSECFG) tests/unit
UNIT_TEST_COV_CMD = $(REPO_ROOT) coverage run -m nose $(NOSECFG) tests/unit && coverage report && coverage html
UNIT_TEST_CMD = $(REPO_ROOT) pytest $(PYTESTCFG) tests/unit
UNIT_TEST_COV_CMD = $(REPO_ROOT) coverage run -m pytest $(PYTESTCFG) tests/unit && coverage report && coverage html

test_unit:
$(UNIT_TEST_CMD)
Expand All @@ -55,7 +55,7 @@ test_unit_cov_in_env: venv_dev
$(ACTIVATE_DEV_VENV) && $(UNIT_TEST_COV_CMD)

FUNCTIONAL_TEST_CMD = $(REPO_ROOT) $(TEST_PROJECT) \
nosetests $(NOSECFG) tests/functional || exit 1
pytest $(PYTESTCFG) tests/functional || exit 1

test_functional:
${PYTHON_BIN} $(ROOT)/scripts/test/check_clang.py || exit 1;
Expand Down
37 changes: 2 additions & 35 deletions analyzer/tests/functional/analyze/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,5 @@
#
# -------------------------------------------------------------------------

"""Setup for the test package analyze.
"""


import os
import shutil

from libtest import env


# Test workspace should be initialized in this module.
TEST_WORKSPACE = None


def setup_package():
"""Setup the environment for the tests."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('analyze')

report_dir = os.path.join(TEST_WORKSPACE, 'reports')
os.makedirs(report_dir)

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE


def teardown_package():
"""Delete the workspace associated with this test"""

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

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)
# This file is empty, and is only present so that this directory will form a
# package.
29 changes: 22 additions & 7 deletions analyzer/tests/functional/analyze/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,26 @@
class TestAnalyze(unittest.TestCase):
_ccClient = None

def setUp(self):
def setup_class(self):
"""Setup the environment for the tests."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('analyze')

report_dir = os.path.join(TEST_WORKSPACE, 'reports')
os.makedirs(report_dir)

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE

def teardown_class(self):
"""Delete the workspace associated with this test"""

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)

def setup_method(self, method):
"""Setup the environment for the tests."""

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

test_class = self.__class__.__name__
Expand All @@ -51,7 +68,7 @@ def setUp(self):
self.disabling_modeling_checker_regex = re.compile(
r"analyzer-disable-checker=.*unix.cstring.CStringModeling.*")

def tearDown(self):
def teardown_method(self, method):
"""Restore environment after tests have ran."""
os.chdir(self.__old_pwd)
if os.path.isdir(self.report_dir):
Expand Down Expand Up @@ -1105,11 +1122,9 @@ def test_analyzer_and_checker_config(self):

# It's printed as a found report and in the checker statistics.
# Note: If this test case fails, its pretty sure that something totally
# unrelated to the analysis broke in CodeChecker. Comment out the line
# starting with 'nocapture' in 'analyzer/.noserc', and print both the
# unrelated to the analysis broke in CodeChecker. Print both the
# stdout and stderr streams from the above communicate() call (the
# latter of which is ignored with _ above)
# Put a + if the above instructions saved you: +
# latter of which is ignored with _ above).
self.assertEqual(out.count('hicpp-use-nullptr'), 2)

analyze_cmd = [self._codechecker_cmd, "check", "-l", build_json,
Expand Down
31 changes: 2 additions & 29 deletions analyzer/tests/functional/analyze_and_parse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,5 @@
#
# -------------------------------------------------------------------------

"""Setup for the test package 'analyze' and 'parse'."""


import os
import shutil

from libtest import env

TEST_WORKSPACE = None


def setup_package():
"""Setup the environment for the tests."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('analyze_and_parse')

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE


def teardown_package():
"""Delete the workspace associated with this test"""

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

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)
# This file is empty, and is only present so that this directory will form a
# package.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@


class AnalyzeParseTestCaseMeta(type):

def __new__(mcs, name, bases, test_dict):

def gen_test(path, mode):
Expand Down Expand Up @@ -64,7 +65,11 @@ class AnalyzeParseTestCase(
def setup_class(cls):
"""Setup the class."""

# TEST_WORKSPACE is automatically set by test package __init__.py
global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('analyze_and_parse')

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE

test_workspace = os.environ['TEST_WORKSPACE']
cls.test_workspaces = {'NORMAL': os.path.join(test_workspace,
'NORMAL'),
Expand Down Expand Up @@ -108,7 +113,14 @@ def teardown_class(cls):
"""Restore environment after tests have ran."""
os.chdir(cls.__old_pwd)

def tearDown(self):
# TODO: If environment variable is set keep the workspace
# and print out the path.
global TEST_WORKSPACE

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)

def teardown_method(self, method):
"""Restore environment after a particular test has run."""
output_dir = AnalyzeParseTestCase.test_workspaces['OUTPUT']
if os.path.isdir(output_dir):
Expand Down
34 changes: 2 additions & 32 deletions analyzer/tests/functional/cmdline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,5 @@
#
# -------------------------------------------------------------------------

"""Setup for the test package analyze.
"""


import os
import shutil

from libtest import env


# Test workspace should be initialized in this module.
TEST_WORKSPACE = None


def setup_package():
"""Setup the environment for the tests."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('cmdline')

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE


def teardown_package():
"""Delete the workspace associated with this test"""

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

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)
# This file is empty, and is only present so that this directory will form a
# package.
21 changes: 20 additions & 1 deletion analyzer/tests/functional/cmdline/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import json
import os
import shutil
import subprocess
import unittest

Expand All @@ -38,7 +39,25 @@ class TestCmdline(unittest.TestCase):
Simple tests to check CodeChecker command line.
"""

def setUp(self):
def setup_class(self):
"""Setup the environment for the tests."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('cmdline')

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE

def teardown_class(self):
"""Delete the workspace associated with this test"""

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

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)

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

Expand Down
34 changes: 2 additions & 32 deletions analyzer/tests/functional/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,5 @@
#
# -------------------------------------------------------------------------

"""Setup for the config test for the analyze command.
"""


import os
import shutil

from libtest import env


# Test workspace should be initialized in this module.
TEST_WORKSPACE = None


def setup_package():
"""Setup the environment for the tests."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('config')

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE


def teardown_package():
"""Delete the workspace associated with this test"""

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

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)
# This file is empty, and is only present so that this directory will form a
# package.
21 changes: 20 additions & 1 deletion analyzer/tests/functional/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import json
import os
import shutil
import subprocess
import unittest

Expand All @@ -23,7 +24,25 @@
class TestConfig(unittest.TestCase):
_ccClient = None

def setUp(self):
def setup_class(self):
"""Setup the environment for the tests."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('config')

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE

def teardown_class(self):
"""Delete the workspace associated with this test"""

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

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)

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

0 comments on commit c8db700

Please sign in to comment.