Skip to content

how can I dynamically remove a marker? #3324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
satishmovvar opened this issue Mar 19, 2018 · 2 comments
Closed

how can I dynamically remove a marker? #3324

satishmovvar opened this issue Mar 19, 2018 · 2 comments

Comments

@satishmovvar
Copy link

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)
	
@satishmovvar
Copy link
Author

After further investigation I found item maintains its markers under "keywords._markers" dictionary.

@RonnyPfannschmidt
Copy link
Member

@satishmovvar there is no support to dynamically remove markers, and the data structures in the keywords mapping are polymorphic meaning its very likely you will end up with a broken code unless you are really really carefull

in addition markers and their handling will soon moved to a own internal structure meaning your code wont work with the next pytest feature release

both the new storage and the current storage are not meant to be modified from the outside - so expect breakage

please consider introducing a custom mark and adding skip markers for tests the user wants to skip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants