PyTest: fix repeating code and remove dependencies










1














I am writing tests for an API with pytest.
The tests are structured like that:



KEEP_BOX_IDS = ["123abc"]

@pytest.fixture(scope="module")
def s():
UID = os.environ.get("MYAPI_UID")
if UID is None:
raise KeyError("UID not set in environment variable")
PWD = os.environ.get("MYAPI_PWD")
if PWD is None:
raise KeyError("PWD not set in environment variable")
return myapi.Session(UID, PWD)



@pytest.mark.parametrize("name,description,count", [
("Normal Box", "Normal Box Description", 1),
("ÄäÖöÜüß!§", "ÄäÖöÜüß!§", 2),
("___--_?'*#", "nn1738nn", 3),
])
def test_create_boxes(s, name, description, count):
box_info_create = s.create_box(name, description)
assert box_info_create["name"] == name
assert box_info_create["desc"] == description
box_info = s.get_box_info(box_info_create["id"])
assert box_info["name"] == name
assert box_info["desc"] == description
assert len(s.get_box_list()) == count + len(KEEP_BOX_IDS)


def test_update_boxes(s):
bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
new_name = b["name"] + "_updated"
new_desc = b["desc"] + "_updated"
s.update_box(b_id, new_name, new_desc)
box_info = s.get_box_info(b_id)
assert box_info["name"] == new_name
assert get_box_info["desc"] == new_desc


I use a fixture to set up the session (this will keep me connected to the API).



As you can see I am creating 3 boxes at the beginning.
All test that are following do some sort of operations on this 3 boxes. (Boxes are just spaces for folders and files)



For example: update_boxes, create_folders, rename_folders, upload_files, change_file names, etc..



I know it's not good, since all the tests are dependent from each other, but if I execute them in the right order the test is valid and thats enough.



The second issue, which borders me the most, is that all the following tests start with the same lines:



bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
box_info = s.get_box_info(b_id)


I always need to call this for loop to get each boxs id and info.
I've tried to put it in a second fixture, but the problem is that then there will be two fixtures.
Is there a better way of doing this?



Thanks










share|improve this question





















  • Not sure I got it right, but you can create a fixture that uses another fixture as argument, e.g. def box_create(s): .... To break up the dependencies between tests, you should use the fixture finalization, e.g. the fixturebox_create creates the boxes, yields and removes the added boxes after the test finishes. This will ensure that each test starts with the same data setup.
    – hoefling
    Nov 13 at 9:30
















1














I am writing tests for an API with pytest.
The tests are structured like that:



KEEP_BOX_IDS = ["123abc"]

@pytest.fixture(scope="module")
def s():
UID = os.environ.get("MYAPI_UID")
if UID is None:
raise KeyError("UID not set in environment variable")
PWD = os.environ.get("MYAPI_PWD")
if PWD is None:
raise KeyError("PWD not set in environment variable")
return myapi.Session(UID, PWD)



@pytest.mark.parametrize("name,description,count", [
("Normal Box", "Normal Box Description", 1),
("ÄäÖöÜüß!§", "ÄäÖöÜüß!§", 2),
("___--_?'*#", "nn1738nn", 3),
])
def test_create_boxes(s, name, description, count):
box_info_create = s.create_box(name, description)
assert box_info_create["name"] == name
assert box_info_create["desc"] == description
box_info = s.get_box_info(box_info_create["id"])
assert box_info["name"] == name
assert box_info["desc"] == description
assert len(s.get_box_list()) == count + len(KEEP_BOX_IDS)


def test_update_boxes(s):
bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
new_name = b["name"] + "_updated"
new_desc = b["desc"] + "_updated"
s.update_box(b_id, new_name, new_desc)
box_info = s.get_box_info(b_id)
assert box_info["name"] == new_name
assert get_box_info["desc"] == new_desc


I use a fixture to set up the session (this will keep me connected to the API).



As you can see I am creating 3 boxes at the beginning.
All test that are following do some sort of operations on this 3 boxes. (Boxes are just spaces for folders and files)



For example: update_boxes, create_folders, rename_folders, upload_files, change_file names, etc..



I know it's not good, since all the tests are dependent from each other, but if I execute them in the right order the test is valid and thats enough.



The second issue, which borders me the most, is that all the following tests start with the same lines:



bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
box_info = s.get_box_info(b_id)


I always need to call this for loop to get each boxs id and info.
I've tried to put it in a second fixture, but the problem is that then there will be two fixtures.
Is there a better way of doing this?



Thanks










share|improve this question





















  • Not sure I got it right, but you can create a fixture that uses another fixture as argument, e.g. def box_create(s): .... To break up the dependencies between tests, you should use the fixture finalization, e.g. the fixturebox_create creates the boxes, yields and removes the added boxes after the test finishes. This will ensure that each test starts with the same data setup.
    – hoefling
    Nov 13 at 9:30














1












1








1


0





I am writing tests for an API with pytest.
The tests are structured like that:



KEEP_BOX_IDS = ["123abc"]

@pytest.fixture(scope="module")
def s():
UID = os.environ.get("MYAPI_UID")
if UID is None:
raise KeyError("UID not set in environment variable")
PWD = os.environ.get("MYAPI_PWD")
if PWD is None:
raise KeyError("PWD not set in environment variable")
return myapi.Session(UID, PWD)



@pytest.mark.parametrize("name,description,count", [
("Normal Box", "Normal Box Description", 1),
("ÄäÖöÜüß!§", "ÄäÖöÜüß!§", 2),
("___--_?'*#", "nn1738nn", 3),
])
def test_create_boxes(s, name, description, count):
box_info_create = s.create_box(name, description)
assert box_info_create["name"] == name
assert box_info_create["desc"] == description
box_info = s.get_box_info(box_info_create["id"])
assert box_info["name"] == name
assert box_info["desc"] == description
assert len(s.get_box_list()) == count + len(KEEP_BOX_IDS)


def test_update_boxes(s):
bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
new_name = b["name"] + "_updated"
new_desc = b["desc"] + "_updated"
s.update_box(b_id, new_name, new_desc)
box_info = s.get_box_info(b_id)
assert box_info["name"] == new_name
assert get_box_info["desc"] == new_desc


I use a fixture to set up the session (this will keep me connected to the API).



As you can see I am creating 3 boxes at the beginning.
All test that are following do some sort of operations on this 3 boxes. (Boxes are just spaces for folders and files)



For example: update_boxes, create_folders, rename_folders, upload_files, change_file names, etc..



I know it's not good, since all the tests are dependent from each other, but if I execute them in the right order the test is valid and thats enough.



The second issue, which borders me the most, is that all the following tests start with the same lines:



bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
box_info = s.get_box_info(b_id)


I always need to call this for loop to get each boxs id and info.
I've tried to put it in a second fixture, but the problem is that then there will be two fixtures.
Is there a better way of doing this?



Thanks










share|improve this question













I am writing tests for an API with pytest.
The tests are structured like that:



KEEP_BOX_IDS = ["123abc"]

@pytest.fixture(scope="module")
def s():
UID = os.environ.get("MYAPI_UID")
if UID is None:
raise KeyError("UID not set in environment variable")
PWD = os.environ.get("MYAPI_PWD")
if PWD is None:
raise KeyError("PWD not set in environment variable")
return myapi.Session(UID, PWD)



@pytest.mark.parametrize("name,description,count", [
("Normal Box", "Normal Box Description", 1),
("ÄäÖöÜüß!§", "ÄäÖöÜüß!§", 2),
("___--_?'*#", "nn1738nn", 3),
])
def test_create_boxes(s, name, description, count):
box_info_create = s.create_box(name, description)
assert box_info_create["name"] == name
assert box_info_create["desc"] == description
box_info = s.get_box_info(box_info_create["id"])
assert box_info["name"] == name
assert box_info["desc"] == description
assert len(s.get_box_list()) == count + len(KEEP_BOX_IDS)


def test_update_boxes(s):
bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
new_name = b["name"] + "_updated"
new_desc = b["desc"] + "_updated"
s.update_box(b_id, new_name, new_desc)
box_info = s.get_box_info(b_id)
assert box_info["name"] == new_name
assert get_box_info["desc"] == new_desc


I use a fixture to set up the session (this will keep me connected to the API).



As you can see I am creating 3 boxes at the beginning.
All test that are following do some sort of operations on this 3 boxes. (Boxes are just spaces for folders and files)



For example: update_boxes, create_folders, rename_folders, upload_files, change_file names, etc..



I know it's not good, since all the tests are dependent from each other, but if I execute them in the right order the test is valid and thats enough.



The second issue, which borders me the most, is that all the following tests start with the same lines:



bl = s.get_box_list()
for b in bl:
b_id = b['id']
if b_id not in KEEP_BOX_IDS:
box_info = s.get_box_info(b_id)


I always need to call this for loop to get each boxs id and info.
I've tried to put it in a second fixture, but the problem is that then there will be two fixtures.
Is there a better way of doing this?



Thanks







python-3.x rest pytest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 11:16









Kamil Popanda

61




61











  • Not sure I got it right, but you can create a fixture that uses another fixture as argument, e.g. def box_create(s): .... To break up the dependencies between tests, you should use the fixture finalization, e.g. the fixturebox_create creates the boxes, yields and removes the added boxes after the test finishes. This will ensure that each test starts with the same data setup.
    – hoefling
    Nov 13 at 9:30

















  • Not sure I got it right, but you can create a fixture that uses another fixture as argument, e.g. def box_create(s): .... To break up the dependencies between tests, you should use the fixture finalization, e.g. the fixturebox_create creates the boxes, yields and removes the added boxes after the test finishes. This will ensure that each test starts with the same data setup.
    – hoefling
    Nov 13 at 9:30
















Not sure I got it right, but you can create a fixture that uses another fixture as argument, e.g. def box_create(s): .... To break up the dependencies between tests, you should use the fixture finalization, e.g. the fixturebox_create creates the boxes, yields and removes the added boxes after the test finishes. This will ensure that each test starts with the same data setup.
– hoefling
Nov 13 at 9:30





Not sure I got it right, but you can create a fixture that uses another fixture as argument, e.g. def box_create(s): .... To break up the dependencies between tests, you should use the fixture finalization, e.g. the fixturebox_create creates the boxes, yields and removes the added boxes after the test finishes. This will ensure that each test starts with the same data setup.
– hoefling
Nov 13 at 9:30


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261015%2fpytest-fix-repeating-code-and-remove-dependencies%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261015%2fpytest-fix-repeating-code-and-remove-dependencies%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto