Issue
We have projects that require different Conan packages during unit tests (i.e. mocks) than during normal compilation. Is there a best practice how to implement conditional requirements in a conanfile for such a use case?
We have tried using switches with environment variables like this:
class project_a(ConanFile):
....
def requirements()
if os.getenv('UNIT_TESTING') == 'true':
self.requires('mock_for_testing/1.0.0@someuser/somechannel
...
But then the requirements for project_a are always changed during unit testing. So also when a project is tested, that consumes project_a.
Solution
Conan is not focused on running unit test when building packages, it's focused on packaging artifacts like libraries, headers and binaries.
However, there is a prepared environment variable which can be used for unit tests: CONAN_RUN_TESTS. You should use tools.get_env instead.
Also, take a look on self.develop which can be used to detected when you are building or consuming a package, so you can use as condition to add or not a requirement:
def requirements()
if self.develop:
self.requires('mock/1.0.0@user/channel')
Then, the package will be consumed only when running the command create.
Answered By - uilianries
Answer Checked By - Senaida (JavaFixing Volunteer)