[py-dev] Little py.test patch

holger krekel hpk at trillke.net
Fri May 26 10:12:51 CEST 2006


On Thu, May 25, 2006 at 21:35 +0200, Antonio Cuni wrote:
> Hi Holger,
> I've written a little patch for py.test that implements a new feature.

thanks! 

> Basically, I noticed that often I do the following code-and-test cycle, 
> especially after a large refactoring:
>   - edit the code
>   - run all test with -x
>   - correct the bug (ofter a stupid one, like a type)
>   - restart the cycle

did you notice the existing "--looponfailing" option?  
It aims to automize this cycle. 

i am a bit in a rush, sorry and best, 

    holger

 
> I don't like to run all tests without -x because often one small bug 
> affects many tests, so I don't want to wait for them to be completed.
> 
> The problem with my approach is that when the failing tests are at the 
> end of the queue I have to wait long before all previous tests (that I 
> already know to pass) complete, so I wrote some code that let me to 
> start from a specific test (identified by a number) and skip the ones 
> that come before.
> 
> The command-line option to do this is '-n'; when a test fails its number 
> is printed at the start of the traceback, just after the name, so we can 
> easily restart from there.
> 
> I don't know how well written is the patch, because I know very little 
> of py.test internals, it may seem just a quick hack, but it does the job.
> 
> Here is the diff against revision 26654, feel free to include in the 
> repository.
> 
> ciao Anto

> Index: defaultconftest.py
> ===================================================================
> --- defaultconftest.py	(revision 26654)
> +++ defaultconftest.py	(working copy)
> @@ -23,7 +23,10 @@
>                 help="disable catching of sys.stdout/stderr output."),
>          Option('-k', 
>                 action="store", dest="keyword", default='',
> -               help="only run test items matching the given (google-style) keyword expression"), 
> +               help="only run test items matching the given (google-style) keyword expression"),
> +        Option('-n', 
> +               action="store", dest="startfrom", default=0,
> +               help="only run test items whose count number is greater or equal to the given number"),
>          Option('-l', '--showlocals',
>                 action="store_true", dest="showlocals", default=False,
>                 help="show locals in tracebacks (disabled by default)"),
> Index: session.py
> ===================================================================
> --- session.py	(revision 26654)
> +++ session.py	(working copy)
> @@ -7,7 +7,8 @@
>      """
>      def __init__(self, config): 
>          self._memo = []
> -        self.config = config 
> +        self.config = config
> +        self.colitem_count = 0
>  
>      def shouldclose(self): 
>          return False 
> @@ -63,6 +64,9 @@
>          if self.shouldclose(): 
>              raise Exit, "received external close signal" 
>  
> +        colitem.count = self.colitem_count
> +        self.colitem_count += 1
> +
>          outcome = None 
>          colitem.startcapture() 
>          try: 
> @@ -94,6 +98,7 @@
>              return 
>          if isinstance(colitem, py.test.Item): 
>              self.skipbykeyword(colitem)
> +            self.skipbystartfrom(colitem)
>          res = colitem.run() 
>          if res is None: 
>              return py.test.Item.Passed() 
> @@ -124,6 +129,14 @@
>              if not (eor ^ self._matchonekeyword(key, chain)): 
>                  py.test.skip("test not selected by keyword %r" %(keyword,))
>  
> +    def skipbystartfrom(self, colitem):
> +        try:
> +            startfrom = int(self.config.option.startfrom)
> +        except ValueError:
> +            startfrom = 0
> +        if colitem.count < startfrom:
> +            py.test.skip('test not selected by start from %d filter' % startfrom)
> +
>      def _matchonekeyword(self, key, chain): 
>          for subitem in chain: 
>              if subitem.haskeyword(key): 
> Index: terminal/terminal.py
> ===================================================================
> --- terminal/terminal.py	(revision 26654)
> +++ terminal/terminal.py	(working copy)
> @@ -360,9 +360,9 @@
>          #   entry.frame.code.firstlineno != lineno: 
>          #    self.out.line("testcode: %s:%d" % (fn, lineno+1)) 
>          if root == fn: 
> -            self.out.sep("_", "entrypoint: %s" %(modpath))
> +            self.out.sep("_", "entrypoint: %s (%d)" %(modpath, item.count))
>          else:
> -            self.out.sep("_", "entrypoint: %s %s" %(root.basename, modpath))
> +            self.out.sep("_", "entrypoint: %s %s (%d)" %(root.basename, modpath, item.count))
>  
>      def getentrysource(self, entry):
>          try: 


-- 
merlinux GmbH       Steinbergstr. 42    31139 Hildesheim   
http://merlinux.de  tel +49 5121 20800 75 (fax 77) 


More information about the py-dev mailing list