- Mais recentes
- Mais votos
- Mais comentários
Hi Greg_B,
First of all, thank you very much for your reply, I was able to achieve one unit test publishing a message to AWS Cloudwatch metric by using the AWS Greengrass component SDK, yet I have many doubts maybe you can solve me, since I cannot see how this procedure is actually working.
I am getting a passed test for the one previously indicated, which its code is attached below:
def test_ipc_connect(self, mocker): ipc_connect = mocker.patch('awsiot.greengrasscoreipc.connect') mocker.patch('json.loads', return_value='1') init_ipc_client() ret_cpu = publish_message(ipc_connect, aws_cloudwatch_metric_payload("Greengrass-CoreDevice", "System.Cpu.Usage", "hostname", "co2sink-dev-CoreDevice02", get_cpu_usage()), "cloudwatch/metric/put") ipc_connect.assert_called_once() assert ret_cpu == 1
But what really worries me is how it works and how the credentials are internally managed by the mock framework. As fas as I concern, the components that are running within the AWS Greengrass software work since it manages everything by using its own credentials giving at the installation process. What is it really being tested? Is it some kind of simulation?
I have a 75% coverage, which is absolutely rather good, but I am quite demanding, so I would like to have a 90% at least, the problem here is I am not able to test all exceptions that the AWS Greengrasas component SDK might throw.
How can I test those cases? That is to say, the wrong ones (the publish message method is one from your example codes, but modified to fit in my application).
I have tried something like this, but I am sure that is not ok, because in order to achieve the test properly, the exception should be thrown first within the publish_message method:
def test_get_secret_exception(self, mocker): ipc_connect = mocker.patch('awsiot.greengrasscoreipc.connect', side_effect=Exception('mocked exception')) with pytest.raises(Exception) as e: init_ipc_client() publish_message(ipc_connect, aws_cloudwatch_metric_payload("Greengrass-CoreDevice", "System.Cpu.Usage", "hostname", "co2sink-dev-CoreDevice02", get_cpu_usage()), "cloudwatch/metric/put") assert e.type == SystemExit assert e.code == 1 ipc_connect.assert_called_once()
Thanks beforehand, looking forward to hearing from you.
Kind regards.
Daniel.
Hi Daniel,
Not a guide, but you can find a small Python unit test example here, showing how to mock the IPC: https://github.com/awslabs/aws-greengrass-labs-component-for-home-assistant/blob/main/tests/test_artifacts_secret.py
This is the code under test: https://github.com/awslabs/aws-greengrass-labs-component-for-home-assistant/blob/main/artifacts/secret.py
Beyond the IPC, you can find more Python unit tests in the directory: https://github.com/awslabs/aws-greengrass-labs-component-for-home-assistant/tree/main/tests
And in this project: https://github.com/awslabs/aws-greengrass-labs-component-for-the-things-stack-lorawan/tree/main/tests
Hopefully that is enough to get you going.
Conteúdo relevante
- AWS OFICIALAtualizada há 10 meses
- AWS OFICIALAtualizada há 2 anos
- AWS OFICIALAtualizada há 3 anos
- AWS OFICIALAtualizada há um mês
Generally speaking, all of the GG library calls should be mocked so you only test the logic of your code. There's no real credentials being used in your test case.
For each error, you have both a return value and a raise. But the raise will prevent the return value being returned.
To test the raises, you would need to mock the
future_response.result()
call and use a side_effect to throw the right exception.Side effect example here: https://github.com/awslabs/aws-greengrass-labs-component-for-home-assistant/blob/0056ab6f23ac9949cf6d1b561ac6635e756da663/tests/test_artifacts_secret.py#L20
However, you should possibly read more widely on Pytest and mocker as this is all outside of AWS scope.
Hi Greg_B
Thank you very much for your reply, I will try the exception approach you have just mentioned before, but first I have to replace everything developed by pytest to nosetests, since it is the tool used by my work colleagues, so I will let you know how it goes.
Kind regards. Daniel.