conftest.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. import os
  17. def pytest_addoption(parser):
  18. parser.addoption("--output-dir", action="store",
  19. default="/tmp/BlockadeTests",
  20. help="location of output directory where output log and plot files will be created")
  21. parser.addoption("--log-format",
  22. action="store",
  23. default="%(asctime)s|%(levelname)s|%(threadName)s|%(filename)s:%(lineno)s -"
  24. " %(funcName)s()|%(message)s",
  25. help="specify log format")
  26. parser.addoption("--log-level", action="store", default="info", help="specify log level")
  27. def pytest_configure(config):
  28. outputdir = config.option.output_dir
  29. try:
  30. os.makedirs(outputdir)
  31. except OSError, e:
  32. raise Exception(e.strerror + ": " + e.filename)
  33. log_file = os.path.join(outputdir, "output.log")
  34. if config.option.log_level == "trace":
  35. loglevel = eval("logging.DEBUG")
  36. else:
  37. loglevel = eval("logging." + config.option.log_level.upper())
  38. logformatter = logging.Formatter(config.option.log_format)
  39. logging.basicConfig(filename=log_file, filemode='w', level=loglevel, format=config.option.log_format)
  40. console = logging.StreamHandler()
  41. console.setLevel(loglevel)
  42. console.setFormatter(logformatter)
  43. logging.getLogger('').addHandler(console)
  44. def pytest_report_teststatus(report):
  45. logger = logging.getLogger('main')
  46. loc, line, name = report.location
  47. if report.outcome == 'skipped':
  48. pass
  49. elif report.when == 'setup':
  50. logger.info("RUNNING TEST \"%s\" at location \"%s\" at line number \"%s\"" % (name, loc, str(line)))
  51. elif report.when == 'call':
  52. logger.info("TEST \"%s\" %s in %3.2f seconds" % (name, report.outcome.upper(), report.duration))
  53. def pytest_sessionfinish(session):
  54. logger = logging.getLogger('main')
  55. logger.info("ALL TESTS FINISHED")