Suppress PytestCollectionWarning

pytest complains PytestCollectionWarning when a class name looks
like a test case class ("TestFoo").
We can disable the warning by adding '__test__ = False' but
it looks easier to drop "Test" prefix from such classes.

This commit drops "Test" prefix if we can simpy drop it from
class names. If a class name will be too generic after dropping
"Test" prefix, I renamed such classes from "TestFoo" to "FooForTesting"
as a workaround.

Change-Id: I617ab8462a9dc173e34f4aae899e0b0def6ecdc1
This commit is contained in:
Akihiro Motoki 2020-04-27 10:55:34 +09:00
parent f7eaa9138b
commit c34cc5c06f
3 changed files with 122 additions and 125 deletions

View File

@ -209,7 +209,7 @@ class MACAddressFieldTests(test.TestCase):
self.assertEqual(field.mac_address, "00:11:88:99:aa:ff") self.assertEqual(field.mac_address, "00:11:88:99:aa:ff")
class TestChoiceFieldForm(forms.SelfHandlingForm): class ChoiceFieldForm(forms.SelfHandlingForm):
title_dic = {"label1": {"title": "This is choice 1"}, title_dic = {"label1": {"title": "This is choice 1"},
"label2": {"title": "This is choice 2"}, "label2": {"title": "This is choice 2"},
"label3": {"title": "This is choice 3"}} "label3": {"title": "This is choice 3"}}
@ -227,8 +227,7 @@ class TestChoiceFieldForm(forms.SelfHandlingForm):
transform_html_attrs=title_dic.get)) transform_html_attrs=title_dic.get))
def __init__(self, request, *args, **kwargs): def __init__(self, request, *args, **kwargs):
super(TestChoiceFieldForm, self).__init__(request, *args, super(ChoiceFieldForm, self).__init__(request, *args, **kwargs)
**kwargs)
choices = ([('choice1', 'label1'), choices = ([('choice1', 'label1'),
('choice2', 'label2')]) ('choice2', 'label2')])
self.fields['test_choices'].choices = choices self.fields['test_choices'].choices = choices
@ -243,7 +242,7 @@ class ChoiceFieldTests(test.TestCase):
def setUp(self): def setUp(self):
super(ChoiceFieldTests, self).setUp() super(ChoiceFieldTests, self).setUp()
self.form = TestChoiceFieldForm(self.request) self.form = ChoiceFieldForm(self.request)
def _render_form(self): def _render_form(self):
return shortcuts.render(self.request, self.template, return shortcuts.render(self.request, self.template,
@ -261,7 +260,7 @@ class ChoiceFieldTests(test.TestCase):
count=1, html=True) count=1, html=True)
class TestThemableChoiceFieldForm(forms.SelfHandlingForm): class ThemableChoiceFieldForm(forms.SelfHandlingForm):
# It's POSSIBLE to combine this with the test helper form above, but # It's POSSIBLE to combine this with the test helper form above, but
# I fear we'd run into collisions where one test's desired output is # I fear we'd run into collisions where one test's desired output is
# actually within a separate widget's output. # actually within a separate widget's output.
@ -283,8 +282,7 @@ class TestThemableChoiceFieldForm(forms.SelfHandlingForm):
transform_html_attrs=title_dic.get)) transform_html_attrs=title_dic.get))
def __init__(self, request, *args, **kwargs): def __init__(self, request, *args, **kwargs):
super(TestThemableChoiceFieldForm, self).__init__(request, *args, super(ThemableChoiceFieldForm, self).__init__(request, *args, **kwargs)
**kwargs)
choices = ([('choice1', 'label1'), choices = ([('choice1', 'label1'),
('choice2', 'label2')]) ('choice2', 'label2')])
self.fields['test_choices'].choices = choices self.fields['test_choices'].choices = choices
@ -299,7 +297,7 @@ class ThemableChoiceFieldTests(test.TestCase):
def setUp(self): def setUp(self):
super(ThemableChoiceFieldTests, self).setUp() super(ThemableChoiceFieldTests, self).setUp()
self.form = TestThemableChoiceFieldForm(self.request) self.form = ThemableChoiceFieldForm(self.request)
def _render_form(self): def _render_form(self):
return shortcuts.render(self.request, self.template, return shortcuts.render(self.request, self.template,

View File

@ -28,7 +28,7 @@ class FormMixinTests(test.TestCase):
view.kwargs = kwargs view.kwargs = kwargs
view.template_name = 'test_template' view.template_name = 'test_template'
# Note(Itxaka): ModalFormView requires a form_class to behave properly # Note(Itxaka): ModalFormView requires a form_class to behave properly
view.form_class = TestForm view.form_class = FormForTesting
return view return view
def test_modal_form_mixin_hide_true_if_ajax(self): def test_modal_form_mixin_hide_true_if_ajax(self):
@ -68,8 +68,7 @@ class FormMixinTests(test.TestCase):
self.assertEqual(view.template_name, view.get_template_names()) self.assertEqual(view.template_name, view.get_template_names())
class TestForm(forms.SelfHandlingForm): class FormForTesting(forms.SelfHandlingForm):
name = forms.CharField(max_length=255) name = forms.CharField(max_length=255)
def handle(self, request, data): def handle(self, request, data):
@ -84,7 +83,7 @@ class FormErrorTests(test.TestCase):
super(FormErrorTests, self).setUp() super(FormErrorTests, self).setUp()
# Note(Itxaka): We pass data to the form so its bound and has the # Note(Itxaka): We pass data to the form so its bound and has the
# proper cleaned_data fields # proper cleaned_data fields
self.form = TestForm(self.request, data={'fake': 'data'}) self.form = FormForTesting(self.request, data={'fake': 'data'})
def _render_form(self): def _render_form(self):
return shortcuts.render(self.request, self.template, return shortcuts.render(self.request, self.template,

View File

@ -39,13 +39,13 @@ def extra_callback_func(request, context):
return "extra" return "extra"
class TestActionOne(workflows.Action): class ActionOne(workflows.Action):
project_id = forms.ChoiceField(label="Project") project_id = forms.ChoiceField(label="Project")
user_id = forms.ChoiceField(label="User") user_id = forms.ChoiceField(label="User")
class Meta(object): class Meta(object):
name = "Test Action One" name = "Action One"
slug = "test_action_one" slug = "action_one"
def populate_project_id_choices(self, request, context): def populate_project_id_choices(self, request, context):
return [(PROJECT_ID, "test_project")] return [(PROJECT_ID, "test_project")]
@ -57,28 +57,28 @@ class TestActionOne(workflows.Action):
return {"foo": "bar"} return {"foo": "bar"}
class TestActionTwo(workflows.Action): class ActionTwo(workflows.Action):
instance_id = forms.CharField(label="Instance") instance_id = forms.CharField(label="Instance")
class Meta(object): class Meta(object):
name = "Test Action Two" name = "Action Two"
slug = "test_action_two" slug = "action_two"
class TestActionThree(workflows.Action): class ActionThree(workflows.Action):
extra = forms.CharField(widget=forms.widgets.Textarea) extra = forms.CharField(widget=forms.widgets.Textarea)
class Meta(object): class Meta(object):
name = "Test Action Three" name = "Action Three"
slug = "test_action_three" slug = "action_three"
class TestActionFour(workflows.Action): class ActionFour(workflows.Action):
field_four = forms.CharField(widget=forms.widgets.Textarea) field_four = forms.CharField(widget=forms.widgets.Textarea)
class Meta(object): class Meta(object):
name = "Test Action Four" name = "Action Four"
slug = "test_action_four" slug = "action_four"
class AdminAction(workflows.Action): class AdminAction(workflows.Action):
@ -90,12 +90,12 @@ class AdminAction(workflows.Action):
permissions = ("horizon.test",) permissions = ("horizon.test",)
class TestDisabledAction(workflows.Action): class DisabledAction(workflows.Action):
disabled_id = forms.CharField(label="Disabled") disabled_id = forms.CharField(label="Disabled")
class Meta(object): class Meta(object):
name = "Test Action Disabled" name = "Action Disabled"
slug = "test_action_disabled" slug = "action_disabled"
class AdminForbiddenAction(workflows.Action): class AdminForbiddenAction(workflows.Action):
@ -107,13 +107,13 @@ class AdminForbiddenAction(workflows.Action):
policy_rules = (('action', 'forbidden'),) policy_rules = (('action', 'forbidden'),)
class TestStepOne(workflows.Step): class StepOne(workflows.Step):
action_class = TestActionOne action_class = ActionOne
contributes = ("project_id", "user_id") contributes = ("project_id", "user_id")
class TestStepTwo(workflows.Step): class StepTwo(workflows.Step):
action_class = TestActionTwo action_class = ActionTwo
depends_on = ("project_id",) depends_on = ("project_id",)
contributes = ("instance_id",) contributes = ("instance_id",)
connections = {"project_id": connections = {"project_id":
@ -122,29 +122,29 @@ class TestStepTwo(workflows.Step):
"other_callback_func")} "other_callback_func")}
class TestStepThree(workflows.Step): class StepThree(workflows.Step):
action_class = TestActionThree action_class = ActionThree
depends_on = ("project_id",) depends_on = ("project_id",)
contributes = ("extra_data",) contributes = ("extra_data",)
connections = {"project_id": (extra_callback_func,)} connections = {"project_id": (extra_callback_func,)}
after = TestStepOne after = StepOne
before = TestStepTwo before = StepTwo
class TestStepFour(workflows.Step): class StepFour(workflows.Step):
action_class = TestActionFour action_class = ActionFour
contributes = ("field_four",) contributes = ("field_four",)
class AdminStep(workflows.Step): class AdminStep(workflows.Step):
action_class = AdminAction action_class = AdminAction
contributes = ("admin_id",) contributes = ("admin_id",)
after = TestStepOne after = StepOne
before = TestStepTwo before = StepTwo
class TestDisabledStep(workflows.Step): class DisabledStep(workflows.Step):
action_class = TestDisabledAction action_class = DisabledAction
contributes = ("disabled_id",) contributes = ("disabled_id",)
def allowed(self, request): def allowed(self, request):
@ -155,29 +155,29 @@ class AdminForbiddenStep(workflows.Step):
action_class = AdminForbiddenAction action_class = AdminForbiddenAction
class TestWorkflow(workflows.Workflow): class WorkflowForTesting(workflows.Workflow):
slug = "test_workflow" slug = "test_workflow"
default_steps = (TestStepOne, TestStepTwo) default_steps = (StepOne, StepTwo)
class TestWorkflowWithConfig(workflows.Workflow): class WorkflowWithConfig(workflows.Workflow):
slug = "test_workflow" slug = "test_workflow"
default_steps = (TestStepOne,) default_steps = (StepOne,)
class TestWorkflowView(workflows.WorkflowView): class WorkflowViewForTesting(workflows.WorkflowView):
workflow_class = TestWorkflow workflow_class = WorkflowForTesting
template_name = "workflow.html" template_name = "workflow.html"
class TestFullscreenWorkflow(workflows.Workflow): class FullscreenWorkflow(workflows.Workflow):
slug = 'test_fullscreen_workflow' slug = 'test_fullscreen_workflow'
default_steps = (TestStepOne, TestStepTwo) default_steps = (StepOne, StepTwo)
fullscreen = True fullscreen = True
class TestFullscreenWorkflowView(workflows.WorkflowView): class FullscreenWorkflowView(workflows.WorkflowView):
workflow_class = TestFullscreenWorkflow workflow_class = FullscreenWorkflow
template_name = "workflow.html" template_name = "workflow.html"
@ -194,86 +194,86 @@ class WorkflowsTests(test.TestCase):
self._reset_workflow() self._reset_workflow()
def _reset_workflow(self): def _reset_workflow(self):
TestWorkflow._cls_registry = [] WorkflowForTesting._cls_registry = []
def test_workflow_construction(self): def test_workflow_construction(self):
TestWorkflow.register(TestStepThree) WorkflowForTesting.register(StepThree)
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
self.assertQuerysetEqual(flow.steps, self.assertQuerysetEqual(flow.steps,
['<TestStepOne: test_action_one>', ['<StepOne: action_one>',
'<TestStepThree: test_action_three>', '<StepThree: action_three>',
'<TestStepTwo: test_action_two>']) '<StepTwo: action_two>'])
self.assertEqual(set(['project_id']), flow.depends_on) self.assertEqual(set(['project_id']), flow.depends_on)
@test.update_settings(HORIZON_CONFIG={'extra_steps': { @test.update_settings(HORIZON_CONFIG={'extra_steps': {
'horizon.test.unit.workflows.test_workflows.TestWorkflowWithConfig': ( 'horizon.test.unit.workflows.test_workflows.WorkflowWithConfig': (
'horizon.test.unit.workflows.test_workflows.TestStepTwo', 'horizon.test.unit.workflows.test_workflows.StepTwo',
'horizon.test.unit.workflows.test_workflows.TestStepThree', 'horizon.test.unit.workflows.test_workflows.StepThree',
'horizon.test.unit.workflows.test_workflows.TestStepFour', 'horizon.test.unit.workflows.test_workflows.StepFour',
), ),
}}) }})
def test_workflow_construction_with_config(self): def test_workflow_construction_with_config(self):
flow = TestWorkflowWithConfig(self.request) flow = WorkflowWithConfig(self.request)
# NOTE: TestStepThree must be placed between TestStepOne and # NOTE: StepThree must be placed between StepOne and
# TestStepTwo in honor of before/after of TestStepThree. # StepTwo in honor of before/after of StepThree.
self.assertQuerysetEqual(flow.steps, self.assertQuerysetEqual(flow.steps,
['<TestStepOne: test_action_one>', ['<StepOne: action_one>',
'<TestStepThree: test_action_three>', '<StepThree: action_three>',
'<TestStepTwo: test_action_two>', '<StepTwo: action_two>',
'<TestStepFour: test_action_four>', '<StepFour: action_four>',
]) ])
def test_step_construction(self): def test_step_construction(self):
step_one = TestStepOne(TestWorkflow(self.request)) step_one = StepOne(WorkflowForTesting(self.request))
# Action slug is moved from Meta by metaclass, and # Action slug is moved from Meta by metaclass, and
# Step inherits slug from action. # Step inherits slug from action.
self.assertEqual(TestActionOne.name, step_one.name) self.assertEqual(ActionOne.name, step_one.name)
self.assertEqual(TestActionOne.slug, step_one.slug) self.assertEqual(ActionOne.slug, step_one.slug)
# Handlers should be empty since there are no connections. # Handlers should be empty since there are no connections.
self.assertEqual(step_one._handlers, {}) self.assertEqual(step_one._handlers, {})
step_two = TestStepTwo(TestWorkflow(self.request)) step_two = StepTwo(WorkflowForTesting(self.request))
# Handlers should be populated since we do have connections. # Handlers should be populated since we do have connections.
self.assertEqual([local_callback_func, other_callback_func], self.assertEqual([local_callback_func, other_callback_func],
step_two._handlers["project_id"]) step_two._handlers["project_id"])
def test_step_invalid_connections_handlers_not_list_or_tuple(self): def test_step_invalid_connections_handlers_not_list_or_tuple(self):
class InvalidStepA(TestStepTwo): class InvalidStepA(StepTwo):
connections = {'project_id': {}} connections = {'project_id': {}}
class InvalidStepB(TestStepTwo): class InvalidStepB(StepTwo):
connections = {'project_id': ''} connections = {'project_id': ''}
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
InvalidStepA(TestWorkflow(self.request)) InvalidStepA(WorkflowForTesting(self.request))
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
InvalidStepB(TestWorkflow(self.request)) InvalidStepB(WorkflowForTesting(self.request))
def test_step_invalid_connection_handler_not_string_or_callable(self): def test_step_invalid_connection_handler_not_string_or_callable(self):
class InvalidStepA(TestStepTwo): class InvalidStepA(StepTwo):
connections = {'project_id': (None,)} connections = {'project_id': (None,)}
class InvalidStepB(TestStepTwo): class InvalidStepB(StepTwo):
connections = {'project_id': (0,)} connections = {'project_id': (0,)}
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
InvalidStepA(TestWorkflow(self.request)) InvalidStepA(WorkflowForTesting(self.request))
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
InvalidStepB(TestWorkflow(self.request)) InvalidStepB(WorkflowForTesting(self.request))
def test_step_invalid_callback(self): def test_step_invalid_callback(self):
# This should raise an exception # This should raise an exception
class InvalidStep(TestStepTwo): class InvalidStep(StepTwo):
connections = {"project_id": ('local_callback_func',)} connections = {"project_id": ('local_callback_func',)}
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
InvalidStep(TestWorkflow(self.request)) InvalidStep(WorkflowForTesting(self.request))
def test_connection_handlers_called(self): def test_connection_handlers_called(self):
TestWorkflow.register(TestStepThree) WorkflowForTesting.register(StepThree)
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
# This should set the value without any errors, but trigger nothing # This should set the value without any errors, but trigger nothing
flow.context['does_not_exist'] = False flow.context['does_not_exist'] = False
@ -283,12 +283,12 @@ class WorkflowsTests(test.TestCase):
# steps one and two, and one has no handlers, so we should see # steps one and two, and one has no handlers, so we should see
# a response from extra, then one from each of step two's handlers. # a response from extra, then one from each of step two's handlers.
val = flow.context.set('project_id', PROJECT_ID) val = flow.context.set('project_id', PROJECT_ID)
self.assertEqual([('test_action_three', 'extra'), self.assertEqual([('action_three', 'extra'),
('test_action_two', 'one'), ('action_two', 'one'),
('test_action_two', 'two')], val) ('action_two', 'two')], val)
def test_workflow_validation(self): def test_workflow_validation(self):
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
# Missing items fail validation. # Missing items fail validation.
with self.assertRaises(exceptions.WorkflowValidationError): with self.assertRaises(exceptions.WorkflowValidationError):
@ -300,7 +300,7 @@ class WorkflowsTests(test.TestCase):
"instance_id": INSTANCE_ID} "instance_id": INSTANCE_ID}
req = self.factory.post("/", seed) req = self.factory.post("/", seed)
req.user = self.user req.user = self.user
flow = TestWorkflow(req, context_seed={"project_id": PROJECT_ID}) flow = WorkflowForTesting(req, context_seed={"project_id": PROJECT_ID})
for step in flow.steps: for step in flow.steps:
if not step.action.is_valid(): if not step.action.is_valid():
self.fail("Step %s was unexpectedly invalid: %s" self.fail("Step %s was unexpectedly invalid: %s"
@ -312,71 +312,71 @@ class WorkflowsTests(test.TestCase):
self.assertTrue(flow.is_valid()) self.assertTrue(flow.is_valid())
def test_workflow_finalization(self): def test_workflow_finalization(self):
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
self.assertTrue(flow.finalize()) self.assertTrue(flow.finalize())
def test_workflow_view(self): def test_workflow_view(self):
view = TestWorkflowView.as_view() view = WorkflowViewForTesting.as_view()
req = self.factory.get("/") req = self.factory.get("/")
res = view(req) res = view(req)
self.assertEqual(200, res.status_code) self.assertEqual(200, res.status_code)
def test_workflow_registration(self): def test_workflow_registration(self):
req = self.factory.get("/foo") req = self.factory.get("/foo")
flow = TestWorkflow(req) flow = WorkflowForTesting(req)
self.assertQuerysetEqual(flow.steps, self.assertQuerysetEqual(flow.steps,
['<TestStepOne: test_action_one>', ['<StepOne: action_one>',
'<TestStepTwo: test_action_two>']) '<StepTwo: action_two>'])
TestWorkflow.register(TestStepThree) WorkflowForTesting.register(StepThree)
flow = TestWorkflow(req) flow = WorkflowForTesting(req)
self.assertQuerysetEqual(flow.steps, self.assertQuerysetEqual(flow.steps,
['<TestStepOne: test_action_one>', ['<StepOne: action_one>',
'<TestStepThree: test_action_three>', '<StepThree: action_three>',
'<TestStepTwo: test_action_two>']) '<StepTwo: action_two>'])
def test_workflow_unregister_unexisting_workflow(self): def test_workflow_unregister_unexisting_workflow(self):
with self.assertRaises(base.NotRegistered): with self.assertRaises(base.NotRegistered):
TestWorkflow.unregister(TestDisabledStep) WorkflowForTesting.unregister(DisabledStep)
def test_workflow_render(self): def test_workflow_render(self):
TestWorkflow.register(TestStepThree) WorkflowForTesting.register(StepThree)
req = self.factory.get("/foo") req = self.factory.get("/foo")
flow = TestWorkflow(req) flow = WorkflowForTesting(req)
output = http.HttpResponse(flow.render()) output = http.HttpResponse(flow.render())
self.assertContains(output, flow.name) self.assertContains(output, flow.name)
self.assertContains(output, TestActionOne.name) self.assertContains(output, ActionOne.name)
self.assertContains(output, TestActionTwo.name) self.assertContains(output, ActionTwo.name)
self.assertContains(output, TestActionThree.name) self.assertContains(output, ActionThree.name)
def test_has_permissions(self): def test_has_permissions(self):
self.assertQuerysetEqual(TestWorkflow._cls_registry, []) self.assertQuerysetEqual(WorkflowForTesting._cls_registry, [])
TestWorkflow.register(AdminStep) WorkflowForTesting.register(AdminStep)
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
step = AdminStep(flow) step = AdminStep(flow)
self.assertCountEqual(step.permissions, self.assertCountEqual(step.permissions,
("horizon.test",)) ("horizon.test",))
self.assertQuerysetEqual(flow.steps, self.assertQuerysetEqual(flow.steps,
['<TestStepOne: test_action_one>', ['<StepOne: action_one>',
'<TestStepTwo: test_action_two>']) '<StepTwo: action_two>'])
self.set_permissions(['test']) self.set_permissions(['test'])
self.request.user = self.user self.request.user = self.user
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
self.assertQuerysetEqual(flow.steps, self.assertQuerysetEqual(flow.steps,
['<TestStepOne: test_action_one>', ['<StepOne: action_one>',
'<AdminStep: admin_action>', '<AdminStep: admin_action>',
'<TestStepTwo: test_action_two>']) '<StepTwo: action_two>'])
def test_has_allowed(self): def test_has_allowed(self):
TestWorkflow.register(TestDisabledStep) WorkflowForTesting.register(DisabledStep)
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
# Check TestDisabledStep is not included # Check DisabledStep is not included
# even though TestDisabledStep is registered. # even though DisabledStep is registered.
self.assertQuerysetEqual(flow.steps, self.assertQuerysetEqual(flow.steps,
['<TestStepOne: test_action_one>', ['<StepOne: action_one>',
'<TestStepTwo: test_action_two>']) '<StepTwo: action_two>'])
def test_step_is_hidden_on_policy(self): def test_step_is_hidden_on_policy(self):
self.policy_patcher.stop() self.policy_patcher.stop()
@ -387,15 +387,15 @@ class WorkflowsTests(test.TestCase):
return True return True
with mock.patch('openstack_auth.policy.check', policy_check): with mock.patch('openstack_auth.policy.check', policy_check):
TestWorkflow.register(AdminForbiddenStep) WorkflowForTesting.register(AdminForbiddenStep)
flow = TestWorkflow(self.request) flow = WorkflowForTesting(self.request)
output = http.HttpResponse(flow.render()) output = http.HttpResponse(flow.render())
self.assertNotContains(output, AdminForbiddenAction.name) self.assertNotContains(output, AdminForbiddenAction.name)
def test_entry_point(self): def test_entry_point(self):
req = self.factory.get("/foo") req = self.factory.get("/foo")
flow = TestWorkflow(req) flow = WorkflowForTesting(req)
self.assertEqual("test_action_one", flow.get_entry_point()) self.assertEqual("action_one", flow.get_entry_point())
flow = TestWorkflow(req, entry_point="test_action_two") flow = WorkflowForTesting(req, entry_point="action_two")
self.assertEqual("test_action_two", flow.get_entry_point()) self.assertEqual("action_two", flow.get_entry_point())