Skip to content

Commit

Permalink
Fix oppia#6888: Run tests split by classes (oppia#7040)
Browse files Browse the repository at this point in the history
* Run tests split by classes

* Fix Test/Tests issue

* Fix naming

* Fix names and comments

* Fix docstring

* Fix path issue

* Add coverage path to avoid circleci failure

* Try clearing cache

* Try fixing coverage issue

* Check path

* Remove extra path

* Remove restore and save cache

* Remove print

* Try fixing ci

* Remove cache comments

* Revert to lexical form
  • Loading branch information
ankita240796 authored and seanlip committed Jul 1, 2019
1 parent 2c72609 commit f09a67d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/domain/exp_jobs_one_off_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ def test_no_action_is_performed_for_deleted_exploration(self):
self, exp_jobs_one_off.TranslatorToVoiceArtistOneOffJob)


class TestDeleteStateIdMappingModelsOneOffJob(test_utils.GenericTestBase):
class DeleteStateIdMappingModelsOneOffJobTests(test_utils.GenericTestBase):
"""Tests the state ID mapping deletion job."""
def test_job_deletes_all_instances_of_model(self):
exp_models.StateIdMappingModel.create(
Expand Down
8 changes: 4 additions & 4 deletions extensions/answer_summarizers/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _perform_calculation(self, state_answers_dict):
return state_answers_calc_output.calculation_output


class AnswerFrequenciesUnitTestCase(CalculationUnitTestBase):
class AnswerFrequenciesUnitTests(CalculationUnitTestBase):
"""Tests for arbitrary answer frequency calculations."""

CALCULATION_ID = 'AnswerFrequencies'
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_answers_with_ties(self):
self.assertEqual(actual_calc_output.to_raw_type(), expected_calc_output)


class Top5AnswerFrequenciesUnitTestCase(CalculationUnitTestBase):
class Top5AnswerFrequenciesUnitTests(CalculationUnitTestBase):
"""Tests for Top 5 answer frequency calculations."""

CALCULATION_ID = 'Top5AnswerFrequencies'
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_top5_with_ties(self):
self.assertEqual(actual_calc_output.to_raw_type(), expected_calc_output)


class Top10AnswerFrequenciesUnitTestCase(CalculationUnitTestBase):
class Top10AnswerFrequenciesUnitTests(CalculationUnitTestBase):
"""Tests for Top 10 answer frequency calculations."""

CALCULATION_ID = 'Top10AnswerFrequencies'
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_top10_with_ties(self):
self.assertEqual(actual_calc_output.to_raw_type(), expected_calc_output)


class FrequencyCommonlySubmittedElementsUnitTestCase(CalculationUnitTestBase):
class FrequencyCommonlySubmittedElementsUnitTests(CalculationUnitTestBase):
"""This calculation only works on answers which are all lists."""
CALCULATION_ID = 'FrequencyCommonlySubmittedElements'

Expand Down
44 changes: 35 additions & 9 deletions scripts/backend_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,35 @@ def _get_all_test_targets(test_path=None, include_load_tests=True):
"""Returns a list of test targets for all classes under test_path
containing tests.
"""
def _convert_to_test_target(path):
"""Remove the .py suffix and replace all slashes with periods."""
return os.path.relpath(path, os.getcwd())[:-3].replace('/', '.')
def _get_test_target_classes(path):
"""Returns a list of all test classes in a given test file path.
Args:
path: str. The path of the test file from which all test classes
are to be extracted.
Returns:
list. A list of all test classes in a given test file path.
"""
class_names = []
with open(path, 'r') as f:
lines = f.readlines()
for line in lines:
if line.startswith('class'):
start_index = line.find(' ') + 1
end_index = line.find('Tests(')
if end_index >= 0:
class_names.append(line[start_index:end_index + 5])
else:
end_index = line.find('Test(')
if end_index >= 0:
class_names.append(line[start_index:end_index + 4])

test_target_path = os.path.relpath(
path, os.getcwd())[:-3].replace('/', '.')
return [
'%s.%s' % (test_target_path, class_name)
for class_name in class_names]

base_path = os.path.join(os.getcwd(), test_path or '')
result = []
Expand All @@ -220,20 +246,20 @@ def _convert_to_test_target(path):
if any([s in root for s in excluded_dirs]):
continue
if root.endswith('_test.py'):
result.append(_convert_to_test_target(
os.path.join(base_path, root)))
result = result + (
_get_test_target_classes(os.path.join(base_path, root)))
for subroot, _, files in os.walk(os.path.join(base_path, root)):
if _LOAD_TESTS_DIR in subroot and include_load_tests:
for f in files:
if f.endswith('_test.py'):
result.append(_convert_to_test_target(
os.path.join(subroot, f)))
result = result + (
_get_test_target_classes(os.path.join(subroot, f)))

for f in files:
if (f.endswith('_test.py') and
os.path.join('core', 'tests') not in subroot):
result.append(_convert_to_test_target(
os.path.join(subroot, f)))
result = result + (
_get_test_target_classes(os.path.join(subroot, f)))

return result

Expand Down

0 comments on commit f09a67d

Please sign in to comment.