Esempio n. 1
0
def test_engine_stopped_suites_should_run(get_crawler, suites):
    """The suites defined at engine_stopped_suites should be loaded and run"""
    crawler = get_crawler()
    spidermon = Spidermon(crawler, engine_stopped_suites=suites)
    spidermon.engine_stopped_suites[0].run = mock.MagicMock()
    spidermon.engine_stopped()
    assert spidermon.engine_stopped_suites[0].__class__.__name__ == "Suite01"
    spidermon.engine_stopped_suites[0].run.assert_called_once_with(mock.ANY)
Esempio n. 2
0
    def run_test(self, **kwargs):
        dt = TestData(**kwargs)
        settings = {
            "SPIDERMON_ENABLED": True,
            "SPIDERMON_SPIDER_OPEN_EXPRESSION_MONITORS": [
                {"tests": [{"expression": dt.expression}]}
            ],
        }
        settings.update(dt.settings)
        crawler = get_crawler(settings_dict=settings)
        crawler.stats.get_stats = lambda _: dt.stats
        spidermon = Spidermon.from_crawler(crawler)
        spider = Spider(name=self.spider_name)

        # mocking, to see test results via raising AssertionError exception
        # with failures and errors as results
        spidermon._run_suites = partial(_test_run_suites, spidermon)

        try:
            spidermon.spider_opened(spider)
        except AssertionError as e:
            failures, errors = e.args[0]
            for f in failures:
                _, trace = f
                raise AssertionError(trace)
            for e in errors:
                _, trace = e
                if dt.expected_error and dt.expected_error in trace:
                    dt.expected_error = None
                else:
                    raise AssertionError(trace)
            if dt.expected_error:
                raise AssertionError(
                    f"Expected error <{dt.expected_error}> was not raised"
                )
Esempio n. 3
0
def test_engine_stopped_suites_should_run_from_signal(get_crawler, suites):
    """The suites defined at SPIDERMON_ENGINE_STOP_MONITORS setting should be loaded and run"""
    settings = {"SPIDERMON_ENGINE_STOP_MONITORS": suites}
    crawler = get_crawler(settings)
    spidermon = Spidermon.from_crawler(crawler)
    spidermon.engine_stopped_suites[0].run = mock.MagicMock()
    crawler.signals.send_catch_log(signal=signals.engine_stopped,
                                   spider=crawler.spider)
    spidermon.engine_stopped_suites[0].run.assert_called_once_with(mock.ANY)
Esempio n. 4
0
def test_spider_closed_suites_should_run(get_crawler, suites):
    """The suites defined at spider_closed_suites should be loaded and run"""
    crawler = get_crawler()
    spidermon = Spidermon(crawler,
                          spider_opened_suites=suites,
                          spider_closed_suites=suites)
    spidermon.spider_closed_suites[0].run = mock.MagicMock()
    spidermon.spider_opened(crawler.spider)
    spidermon.spider_closed(crawler.spider)
    assert spidermon.spider_closed_suites[0].__class__.__name__ == "Suite01"
    spidermon.spider_closed_suites[0].run.assert_called_once_with(mock.ANY)