[TIP] REG: Passing custom parameters to pytest

Florian Bruhin me at the-compiler.org
Tue Sep 12 14:39:08 PDT 2017


Hey,

[I found this mail in my drafts folder, and I think I've never sent it out -
sorry if that's not the case!]

On Wed, Aug 23, 2017 at 11:42:39PM +0530, arun kali raja wrote:
> HI all..
> 
> My testcases 'may' need some dynamic variables that are passed during
> invocation..
> 
> I saw pytest-variables plugin that does something similar.. The only
> disadvantage it accepts only a file.. Doesnt accept a dictionary or json.
> 
> So i was thinking i will have a pytest option to accept a dict or json  (as
> string and then literal-eval). Once it is evaluated i can write it into a
> file and then pass that parameter into --variables . I was thinking of
> using the hook "pytest_cmdline_preparse" hook to do this data conversion .
> 
> Though its working fine i see that this particular hook is marked as
> deprecated in the hookspec.
> 
> Strangely "pytest_cmdline_parse"   hook doesnt get called at all for me.
> 
> What are the other hooks i can use to do this manipulation?
> 
> Or is there a better way altogether to do this..

This makes me wonder whether it'd be easier to either just reimplement
the bit of logic you need, or to get pytest-variables to cooperate with
some code in your conftest.py. I've never used it though, and it looks
like Dave Hunt who wrote it isn't on this list. You might want to open
an issue in its issue tracker at
https://github.com/pytest-dev/pytest-variables/

On Wed, Aug 23, 2017 at 08:33:57PM +0000, Bruno Oliveira wrote:
> You mean passing a json or dict literal in the command line? If so it
> doesn't seem like a good idea, dicts/json will contain a lot of special
> characters that need to be shell-escaped.

At least on unix, just enclose the json in single quotes - as JSON
itself only uses double-quotes, and everything is literal inside single
quotes, that works out pretty well ;-)

Florian

-- 
https://www.qutebrowser.org  | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072  | https://the-compiler.org/pubkey.asc
         I love long mails!  | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20170912/f9c77447/attachment.pgp>


More information about the testing-in-python mailing list