Skip to content

Isolate common test functions in Modbus#33447

Merged
Adminiuga merged 1 commit intohome-assistant:devfrom
janiversen:modbustest
Apr 5, 2020
Merged

Isolate common test functions in Modbus#33447
Adminiuga merged 1 commit intohome-assistant:devfrom
janiversen:modbustest

Conversation

@janiversen
Copy link
Copy Markdown
Member

@janiversen janiversen commented Mar 30, 2020

Proposed change

Since all entity test functions are going to use the modbus class, isolate the common parts in
conftest.py, and thereby make it simpler to write additional test cases.

cleaned up test_modbus_sensor.py while splitting the code.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml

Additional information

pytest .../test_modbus_sensor.py
to run the test.

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@probot-home-assistant probot-home-assistant Bot added has-tests small-pr PRs with less than 30 lines. labels Mar 30, 2020
@janiversen
Copy link
Copy Markdown
Member Author

@vzahradnik would you mind taking a look ? also it would be nice if you included a climate test in your PR.

@vzahradnik
Copy link
Copy Markdown
Contributor

@janiversen I'm not much familiar with Python testing yet, and as such I can't review your code at the moment. My plan regarding Modbus is to finally start upstreaming the rest of my changes, especially new entities, and then I will try to write some tests for them. I will need some time to study how it's done.

@janiversen
Copy link
Copy Markdown
Member Author

@vzahradnik one way of learning unit-testing is to read code, that you how should work :-) When you start writing upstreaming, I am happy to both review your code and help you with writing tests.

@vzahradnik
Copy link
Copy Markdown
Contributor

@janiversen thanks! I will gladly use your offer.

@springstan springstan changed the title Modbus: isolate common test functions Isolate common test functions in Modbus Mar 31, 2020
@Adminiuga
Copy link
Copy Markdown
Contributor

Try rebasing against latest dev. The tests seem to be failing in TTS, which is unrelated to this PR.

Since all entity test functions are going to use
the modbus class, isolate the common parts in
conftest.py, and thereby make it simpler to write
additional test cases.

cleaned up test_modbus_sensor.py while splitting the code.
@janiversen
Copy link
Copy Markdown
Member Author

@Adminiuga done, let us see if this solves the problem

@janiversen
Copy link
Copy Markdown
Member Author

@Adminiuga all checks are green.

Copy link
Copy Markdown
Contributor

@Adminiuga Adminiuga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@Adminiuga Adminiuga merged commit 738ef38 into home-assistant:dev Apr 5, 2020
async def simulate_read_registers(unit, address, count):
"""Simulate modbus register read."""
del unit, address, count # not used in simulation, but in real connection
global read_result
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to use global? Can we instead move this function inside run_test?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, moving the function inside run_test, will work as long as it is called from within run_test, but it is called from core, so it will not have a reference to run_test local variables, at least as I understand it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simulate_read_registers will remember its scope, so it should work to move it inside.

return hub


common_register_config = {CONF_NAME: "test-config", CONF_REGISTER: 1234}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants should be all caps, ie COMMON_REGISTER_CONFIG.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct, an even better solution is to remove the variable since it is unused. This is done in a PR I am preparing.

@janiversen janiversen deleted the modbustest branch April 6, 2020 07:46
@balloob balloob added this to the 0.108.2 milestone Apr 10, 2020
balloob pushed a commit that referenced this pull request Apr 10, 2020
Since all entity test functions are going to use
the modbus class, isolate the common parts in
conftest.py, and thereby make it simpler to write
additional test cases.

cleaned up test_modbus_sensor.py while splitting the code.
@balloob balloob mentioned this pull request Apr 10, 2020
@lock lock Bot locked and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants