Skip to content

how can I dynamically remove a marker? #3324

Closed
@satishmovvar

Description

@satishmovvar

I am creating an User interface to select and deselect tests after test collection. I am considering item.add_marker(pytest.mark.skip) to achieve that; But I didn't find remove_marker option in pytest collected item. Why is so? Is there a way I can dynamically remove marker from the collected item?

Below is the custom plugin I developed to show the tests after it gets collected in a UI which has check boxes. I want to provide an option for user to override pytest.mark.skip written in the code to select or deselect tests.
image

import pytest_testView.listViewer as listViewer
import pytest, pdb
import _pytest
import natsort

__dictKeys = ("file", "line", "test")

class ItemViewModel(object):
	def __init__(self, itemObj, outcome=""):
		self.item = itemObj
		self.__outcome = outcome
		self.__notes = ""

	@property
	def file(self):
		return self.item.location[0]
	@property
	def line(self):
		return self.item.location[1] 
	@property
	def test(self):
		return self.item.location[2]
	@property
	def outcome(self):
		return self.__outcome
	@property
	def notes(self):
		return self.__notes

	def isSelected(self):
		if (isinstance(self.item, _pytest.python.Function)):
			if self.item.get_marker("skip"):
				return False
			else:
				return True
		else:
			return None
	def onSelectionChanged(self, state):
		#pdb.set_trace()
		if (isinstance(self.item, _pytest.python.Function)):
			if (state):
				print ("Test is marked to skip; Can't enable in UI")
				#self.item.remove_marker("skip")
				# There is no method to remove the marker
			else:
				self.item.add_marker("skip")

def makeTestItemCollection(l_testItems):
	#for item in l_testItems:
	#	yield ItemViewModel(item)

	l_d_testObjs = []
	for item in l_testItems:
		l_d_testObjs.append(ItemViewModel(item))
	return l_d_testObjs

def showTestItems(l_testItems):
	__frameBuilder = listViewer.FrameBuilder()
	__frameBuilder.showTests(makeTestItemCollection(l_testItems))
	__frameBuilder.showFrame()
	
def pytest_addoption(parser):
    """Add the '--reorder' argument to the py.test invocation."""
    group = parser.getgroup('tests viewer', 'viewTests', after='general')
    group.addoption(
        '--viewTests', type=str, nargs='*',
        help=(
            "Shows the tests collected in a viewer after the tests are collected "
         )
    )
	
@pytest.hookimpl(trylast=True) # This is mandatory so that the sorting happens last
def pytest_collection_modifyitems(session, config, items):
	items.sort(key=natsort.natsort_keygen(key=lambda x: x._genid))
	items.sort(key=natsort.natsort_keygen(key=lambda x: x.location[0]))
	#------- SORT TESTS -----
	
	viewerOption = config.getoption("viewTests")
	if (viewerOption == None):
		return
	else:
		showTestItems(items)
	

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions