【问题标题】:How Can I call a function with values that will be executed only once when I have several parameters in pytestHow Can I call a function with values that will be executed only once when I have several parameters in pytest
【发布时间】:2022-12-01 22:41:27
【问题描述】:

I run pytest with several params:

@pytest.mark.parametrize('signature_algorithm, cipher, name', [
    pytest.param(rsa_pss_rsae_sha256, AES128-GCM-SHA256, "KEY1"),
    pytest.param(rsa_pss_rsae_sha384, AES128-GCM-SHA256, "KEY2"),
    ....
    def test(signature_algorithm, cipher, cert_name, functaion1("KEY"):
    .....

functaion1(data):
Change file in server (using data value)

I need to call functaion1(data) only once when test starts. How can I achieve this using pytest?

I tried to add function1 as parameter in test function but I got syntax error while I add parentheses to the function1 (because I want to send a certain value to function1).

【问题讨论】:

  • What is the difference between functaion1 and function1?
  • you are missing a closing parentheses ) at the definition of the test test()

标签: python pytest


【解决方案1】:

First of all, I recommend you to read the documentation https://docs.pytest.org/en/6.2.x/parametrize.html

My spider sense says "KEY1K" is a project requirement. Therefore, you can either keep or remove it from parametrization and setup on test function begin

@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])


@pytest.mark.parametrize('signature_algorithm, cipher, expected_value', [
    pytest.param(rsa_pss_rsae_sha256, AES128-GCM-SHA256, 'expected_result_1'),
    pytest.param(rsa_pss_rsae_sha384, AES128-GCM-SHA256, 'expected_result_2')
]):
def test(signature_algorithm, cipher) :
    do_stuff("KEY1K")
    # Do test stuff
    undo_stuff("KEY1K")

【讨论】:

  • Thank you for the answer! Actually, the 'name' parameter is different from what I send to the function1 I send to function1 the file name that the server should switch. in the 'name' parameter I use during the program, I have several values for 'name' parameter, I didn't copy all I copy just the first lines
  • Integration tests are headaches. I prefer to test them either locally or mocked and failed communication corner cases separately.
猜你喜欢
  • 2022-12-26
  • 2022-11-20
  • 2019-09-29
  • 2022-12-02
  • 2022-12-01
  • 2022-12-27
  • 2022-12-02
  • 2022-12-01
  • 1970-01-01
相关资源
最近更新 更多