How can i pass current (running) test module in fixtures which placed in conftest?












0














I would like to know how to pass current running test module to fixture or how do fixture can know it? (I need to load specific config-file for every test module inside the package).



I can create fixture in every test module, but I want to the more universal solution.
Thanks in advance.










share|improve this question
























  • Do you need to access the name/file path of the current module in fixture?
    – hoefling
    Nov 20 '18 at 9:58
















0














I would like to know how to pass current running test module to fixture or how do fixture can know it? (I need to load specific config-file for every test module inside the package).



I can create fixture in every test module, but I want to the more universal solution.
Thanks in advance.










share|improve this question
























  • Do you need to access the name/file path of the current module in fixture?
    – hoefling
    Nov 20 '18 at 9:58














0












0








0







I would like to know how to pass current running test module to fixture or how do fixture can know it? (I need to load specific config-file for every test module inside the package).



I can create fixture in every test module, but I want to the more universal solution.
Thanks in advance.










share|improve this question















I would like to know how to pass current running test module to fixture or how do fixture can know it? (I need to load specific config-file for every test module inside the package).



I can create fixture in every test module, but I want to the more universal solution.
Thanks in advance.







python-3.x pytest autotest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 8:59









Zain Farooq

1,9362926




1,9362926










asked Nov 20 '18 at 8:57









Rezvanov Maxim

1216




1216












  • Do you need to access the name/file path of the current module in fixture?
    – hoefling
    Nov 20 '18 at 9:58


















  • Do you need to access the name/file path of the current module in fixture?
    – hoefling
    Nov 20 '18 at 9:58
















Do you need to access the name/file path of the current module in fixture?
– hoefling
Nov 20 '18 at 9:58




Do you need to access the name/file path of the current module in fixture?
– hoefling
Nov 20 '18 at 9:58












1 Answer
1






active

oldest

votes


















1














Generally, you can get module name into fixture like that:



@pytest.fixture
def fixture_global(request):
module_name = request.module.__name__
print(module_name)
# some logic depends on module name


It may be even global fixture in conftest but this way does not work with session scope fixtures because they are not called every module which use it.





If you want to have a base shared fixture code and additionally some specific code for specific modules I would suggest a better way.



In global conftest.py put "base" fixture with universal code. If you want use it into specific test module, just inject global fixture to local fixture as argument. Something like that:



conftest.py



@pytest.fixture
def global_fixture():
# universal code for vary modules
return universal_obj


test_module.py



@pytest.fixture
def local_fixture(global_fixture):
# specific code that uses global fixture result





share|improve this answer























  • that's exactly what i want. Thank you!
    – Rezvanov Maxim
    Nov 20 '18 at 11:52











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%2f53389396%2fhow-can-i-pass-current-running-test-module-in-fixtures-which-placed-in-conftes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Generally, you can get module name into fixture like that:



@pytest.fixture
def fixture_global(request):
module_name = request.module.__name__
print(module_name)
# some logic depends on module name


It may be even global fixture in conftest but this way does not work with session scope fixtures because they are not called every module which use it.





If you want to have a base shared fixture code and additionally some specific code for specific modules I would suggest a better way.



In global conftest.py put "base" fixture with universal code. If you want use it into specific test module, just inject global fixture to local fixture as argument. Something like that:



conftest.py



@pytest.fixture
def global_fixture():
# universal code for vary modules
return universal_obj


test_module.py



@pytest.fixture
def local_fixture(global_fixture):
# specific code that uses global fixture result





share|improve this answer























  • that's exactly what i want. Thank you!
    – Rezvanov Maxim
    Nov 20 '18 at 11:52
















1














Generally, you can get module name into fixture like that:



@pytest.fixture
def fixture_global(request):
module_name = request.module.__name__
print(module_name)
# some logic depends on module name


It may be even global fixture in conftest but this way does not work with session scope fixtures because they are not called every module which use it.





If you want to have a base shared fixture code and additionally some specific code for specific modules I would suggest a better way.



In global conftest.py put "base" fixture with universal code. If you want use it into specific test module, just inject global fixture to local fixture as argument. Something like that:



conftest.py



@pytest.fixture
def global_fixture():
# universal code for vary modules
return universal_obj


test_module.py



@pytest.fixture
def local_fixture(global_fixture):
# specific code that uses global fixture result





share|improve this answer























  • that's exactly what i want. Thank you!
    – Rezvanov Maxim
    Nov 20 '18 at 11:52














1












1








1






Generally, you can get module name into fixture like that:



@pytest.fixture
def fixture_global(request):
module_name = request.module.__name__
print(module_name)
# some logic depends on module name


It may be even global fixture in conftest but this way does not work with session scope fixtures because they are not called every module which use it.





If you want to have a base shared fixture code and additionally some specific code for specific modules I would suggest a better way.



In global conftest.py put "base" fixture with universal code. If you want use it into specific test module, just inject global fixture to local fixture as argument. Something like that:



conftest.py



@pytest.fixture
def global_fixture():
# universal code for vary modules
return universal_obj


test_module.py



@pytest.fixture
def local_fixture(global_fixture):
# specific code that uses global fixture result





share|improve this answer














Generally, you can get module name into fixture like that:



@pytest.fixture
def fixture_global(request):
module_name = request.module.__name__
print(module_name)
# some logic depends on module name


It may be even global fixture in conftest but this way does not work with session scope fixtures because they are not called every module which use it.





If you want to have a base shared fixture code and additionally some specific code for specific modules I would suggest a better way.



In global conftest.py put "base" fixture with universal code. If you want use it into specific test module, just inject global fixture to local fixture as argument. Something like that:



conftest.py



@pytest.fixture
def global_fixture():
# universal code for vary modules
return universal_obj


test_module.py



@pytest.fixture
def local_fixture(global_fixture):
# specific code that uses global fixture result






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 10:37

























answered Nov 20 '18 at 9:28









Andrey Glazkov

5371613




5371613












  • that's exactly what i want. Thank you!
    – Rezvanov Maxim
    Nov 20 '18 at 11:52


















  • that's exactly what i want. Thank you!
    – Rezvanov Maxim
    Nov 20 '18 at 11:52
















that's exactly what i want. Thank you!
– Rezvanov Maxim
Nov 20 '18 at 11:52




that's exactly what i want. Thank you!
– Rezvanov Maxim
Nov 20 '18 at 11:52


















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%2f53389396%2fhow-can-i-pass-current-running-test-module-in-fixtures-which-placed-in-conftes%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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy