It works 😀

# content of conftest.py

def pytest_generate_tests(metafunc):
idlist = []
argvalues = []
for scenario in metafunc.cls.scenarios:
idlist.append(scenario[0])
items = scenario[1].items()
argnames = [x[0] for x in items]
argvalues.append(([x[1] for x in items]))
metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")

scenario1 = ('LAN', {'URL': 'www.baidu.com'})
scenario2 = ('WAN', {'URL': 'www.sohu.com'})

# content of a test_

import pytest
from conftest import scenario1, scenario2

class TestLogin(object):
scenarios = [scenario1, scenario2]

def test_login_01(self, URL):
assert "www" in URL

def test_login_02(self, URL):
assert "ok" == "ok"

def test_login_03(self, URL):
assert "sohu" in URL


----
Now I'm having the tests...

rx:pytest_proj reed$ pytest --collect-only tests/test_api01.py
============================================================= test session
starts =============================================================
platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /Users/reed/Documents/dev/pytest_proj, inifile:
collected 6 items
<Module 'tests/test_api01.py'>
  <Class 'TestLogin'>
    <Instance '()'>
      <Function 'test_login_01[LAN]'>
      <Function 'test_login_02[LAN]'>
      <Function 'test_login_03[LAN]'>
      <Function 'test_login_01[WAN]'>
      <Function 'test_login_02[WAN]'>
      <Function 'test_login_03[WAN]'>

Thanks Bruno and PYTEST!!

On Fri, May 11, 2018 at 8:56 PM, Reed Xia <huaqin2...@gmail.com> wrote:

> Wow.. sound great! Thanks Bruno, I'll check!
>
> On Fri, May 11, 2018 at 6:19 PM, Bruno Oliveira <nicodde...@gmail.com>
> wrote:
>
>> Hi Reed,
>>
>> You should take a look at parametrizing[1] so you end up with only:
>>
>> /api_test
>>    test_device_api01.py
>>    test_device_api02.py
>>    test_device_api03.py
>>
>> And inside it all tests are parametrized based on LAN_URL and WAN_URL
>> (possibly using a parametrized fixture if it is more convenient).
>>
>> Report generation will remain unaffected.
>>
>> Hope this helps,
>> Bruno.
>>
>> [1] https://docs.pytest.org/en/latest/parametrize.html
>>
>> On Fri, May 11, 2018 at 12:38 AM Reed Xia <huaqin2...@gmail.com> wrote:
>>
>>> Hi,
>>>
>>> In my project there are some API tests(with pytest) like following:
>>>
>>> /api_test
>>>    test_device_api01_via_lan.py
>>>    test_device_api02_via_lan.py
>>>    test_device_api03_via_lan.py
>>>
>>>    test_device_api01_via_wan.py
>>>    test_device_api02_via_wan.py
>>>    test_device_api03_via_wan.py
>>>
>>> As you see and you may guess, yes...the two set of test_*.py have the
>>> almost the same code, except they use the different global variable, one is
>>> LAN_URL the other is WAN_URL, looks stupid, right? :(
>>>
>>> So I want to refactor it, I think the two set of tests should not be
>>> seperated just because of an variable.
>>>
>>> but there's another problem, this test project run with Jenkins,
>>> launched by "python3 -m py.test --junitxml=./result/api_test_result.xml",so
>>> that Jenkins can collect the JUnit formate xml file.
>>>
>>> I think I easily to launch the tests in loop by giving LAN_URL and
>>> WAN_URL, but I didn't figure out how to generate a JUnit XML file in this
>>> way, can someone please share some ideas?
>>>
>>> Thanks,
>>> Reed
>>>
>>>
>>> _______________________________________________
>>> pytest-dev mailing list
>>> pytest-dev@python.org
>>> https://mail.python.org/mailman/listinfo/pytest-dev
>>>
>>
>
_______________________________________________
pytest-dev mailing list
pytest-dev@python.org
https://mail.python.org/mailman/listinfo/pytest-dev

Reply via email to