Statistics: add further test cases
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pynguin. If not, see <https://www.gnu.org/licenses/>.
+from unittest.mock import MagicMock
+
import pytest
import pynguin.configuration as config
+import pynguin.ga.chromosome as chrom
+import pynguin.testsuite.testsuitechromosome as tsc
from pynguin.utils.statistics.searchstatistics import SearchStatistics
from pynguin.utils.statistics.statistics import RuntimeVariable
from pynguin.utils.statistics.statisticsbackend import (
@@ -29,6 +33,16 @@ def search_statistics():
return SearchStatistics()
+@pytest.fixture
+def chromosome():
+ return tsc.TestSuiteChromosome()
+
+
+@pytest.fixture
+def chromosome_mock():
+ return MagicMock(chrom.Chromosome)
+
+
@pytest.mark.parametrize(
"backend, type_",
[
@@ -81,3 +95,51 @@ def test_write_statistics_no_backend():
def test_write_statistics_no_individual(search_statistics):
assert not search_statistics.write_statistics()
+
+
+def test_write_statistics_with_individual(capsys, chromosome):
+ config.INSTANCE.statistics_backend = config.StatisticsBackend.CONSOLE
+ statistics = SearchStatistics()
+ statistics.current_individual(chromosome)
+ result = statistics.write_statistics()
+ captured = capsys.readouterr()
+ assert result
+ assert captured.out == ""
+ assert captured.err.strip() == "No obtained value for output variable TARGET_CLASS"
+
+
+def test_get_output_variables(chromosome, search_statistics):
+ config.INSTANCE.output_variables = (
+ "Coverage,CoverageTimeline," "Length,configuration_id"
+ )
+ config.INSTANCE.budget = 0.005
+ search_statistics.set_output_variable_for_runtime_variable(
+ RuntimeVariable.CoverageTimeline, 0.25
+ )
+ search_statistics.set_output_variable_for_runtime_variable(
+ RuntimeVariable.Coverage, 0.75
+ )
+ search_statistics.set_output_variable_for_runtime_variable(
+ RuntimeVariable.TARGET_CLASS, "foo"
+ )
+ variables = search_statistics._get_output_variables(chromosome, skip_missing=True)
+ assert variables[RuntimeVariable.Coverage.name].value == 0.75
+ assert variables["CoverageTimeline_T1"].value == 0
+ assert variables[RuntimeVariable.Length.name].value == 0
+ assert variables["configuration_id"].value == ""
+
+
+def test_current_individual_no_backend(chromosome):
+ config.INSTANCE.statistics_backend = None
+ statistics = SearchStatistics()
+ assert statistics.current_individual(chromosome) is None
+
+
+def test_current_individual_not_test_suite_chromosome(chromosome_mock):
+ statistics = SearchStatistics()
+ assert statistics.current_individual(chromosome_mock) is None
+
+
+def test_current_individual(chromosome, search_statistics):
+ search_statistics.current_individual(chromosome)
+ assert search_statistics._best_individual == chromosome
