test_test.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import pytest
  3. import torch
  4. from click.testing import CliRunner
  5. from mim.commands.install import cli as install
  6. from mim.commands.test import cli as test
  7. from mim.commands.uninstall import cli as uninstall
  8. def setup_module():
  9. runner = CliRunner()
  10. result = runner.invoke(uninstall, ['mmcv-full', '--yes'])
  11. assert result.exit_code == 0
  12. result = runner.invoke(uninstall, ['mmcls', '--yes'])
  13. assert result.exit_code == 0
  14. @pytest.mark.parametrize('device', [
  15. 'cpu',
  16. pytest.param(
  17. 'cuda',
  18. marks=pytest.mark.skipif(
  19. not torch.cuda.is_available(), reason='requires CUDA support')),
  20. ])
  21. def test_test(device):
  22. runner = CliRunner()
  23. result = runner.invoke(install, ['mmcls', '--yes'])
  24. assert result.exit_code == 0
  25. # Since `mminstall.txt` is not included in the distribution of
  26. # mmcls<=0.23.1, we need to install mmcv-full manually.
  27. result = runner.invoke(install, ['mmcv-full', '--yes'])
  28. assert result.exit_code == 0
  29. result = runner.invoke(test, [
  30. 'mmcls', 'tests/data/lenet5_mnist.py', '--checkpoint',
  31. 'tests/data/epoch_1.pth', f'--device={device}', '--metrics=accuracy'
  32. ])
  33. assert result.exit_code == 0
  34. result = runner.invoke(test, [
  35. 'mmcls', 'tests/data/xxx.py', '--checkpoint', 'tests/data/epoch_1.pth',
  36. f'--device={device}', '--metrics=accuracy'
  37. ])
  38. assert result.exit_code != 0
  39. result = runner.invoke(test, [
  40. 'mmcls', 'tests/data/lenet5_mnist.py', '--checkpoint',
  41. 'tests/data/xxx.pth', f'--device={device}', '--metrics=accuracy'
  42. ])
  43. assert result.exit_code != 0
  44. def teardown_module():
  45. runner = CliRunner()
  46. result = runner.invoke(uninstall, ['mmcv-full', '--yes'])
  47. assert result.exit_code == 0
  48. result = runner.invoke(uninstall, ['mmcls', '--yes'])
  49. assert result.exit_code == 0