Beispiel #1
0
def test_conftestcutdir_inplace_considered(testdir):
    conf = testdir.makeconftest("")
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [conf.dirpath()], confcutdir=conf.dirpath())
    values = conftest._getconftestmodules(conf.dirpath())
    assert len(values) == 1
    assert values[0].__file__.startswith(str(conf))
Beispiel #2
0
def test_doubledash_considered(testdir):
    conf = testdir.mkdir("--option")
    conf.ensure("conftest.py")
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [conf.basename, conf.basename])
    values = conftest._getconftestmodules(conf)
    assert len(values) == 1
 def test_canonical_import(self, monkeypatch):
     mod = types.ModuleType("pytest_xyz")
     monkeypatch.setitem(sys.modules, "pytest_xyz", mod)
     pm = PytestPluginManager()
     pm.import_plugin("pytest_xyz")
     assert pm.get_plugin("pytest_xyz") == mod
     assert pm.is_registered(mod)
Beispiel #4
0
def test_doubledash_considered(testdir: Testdir) -> None:
    conf = testdir.mkdir("--option")
    conf.join("conftest.py").ensure()
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [conf.basename, conf.basename])
    values = conftest._getconftestmodules(py.path.local(conf),
                                          importmode="prepend")
    assert len(values) == 1
Beispiel #5
0
def test_prepared_config(terminal_writer_mock: mock.MagicMock) -> Config:
    config = Config(PytestPluginManager())
    test_pytest_parser = Parser(
        usage="%(prog)s [options] [file_or_dir] [file_or_dir] [...]", processopt=config._processopt
    )
    pytest_addoption(test_pytest_parser)
    config._parser = test_pytest_parser
    return config
Beispiel #6
0
 def test_basic_init(self, basedir: Path) -> None:
     conftest = PytestPluginManager()
     p = basedir / "adir"
     assert (
         conftest._rget_with_confmod("a", p, importmode="prepend", rootpath=basedir)[
             1
         ]
         == 1
     )
Beispiel #7
0
def test_issue151_load_all_conftests(pytester: Pytester) -> None:
    names = "code proj src".split()
    for name in names:
        p = pytester.mkdir(name)
        p.joinpath("conftest.py").touch()

    pm = PytestPluginManager()
    conftest_setinitial(pm, names)
    assert len(set(pm.get_plugins()) - {pm}) == len(names)
Beispiel #8
0
def test_doubledash_considered(pytester: Pytester) -> None:
    conf = pytester.mkdir("--option")
    conf.joinpath("conftest.py").touch()
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [conf.name, conf.name])
    values = conftest._getconftestmodules(conf,
                                          importmode="prepend",
                                          rootpath=pytester.path)
    assert len(values) == 1
Beispiel #9
0
def test_conftestcutdir_inplace_considered(pytester: Pytester) -> None:
    conf = pytester.makeconftest("")
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [conf.parent], confcutdir=conf.parent)
    values = conftest._getconftestmodules(conf.parent,
                                          importmode="prepend",
                                          rootpath=pytester.path)
    assert len(values) == 1
    assert values[0].__file__.startswith(str(conf))
Beispiel #10
0
def test_issue151_load_all_conftests(testdir):
    names = "code proj src".split()
    for name in names:
        p = testdir.mkdir(name)
        p.ensure("conftest.py")

    conftest = PytestPluginManager()
    conftest_setinitial(conftest, names)
    d = list(conftest._conftestpath2mod.values())
    assert len(d) == len(names)
Beispiel #11
0
 def test_immediate_initialiation_and_incremental_are_the_same(self, basedir):
     conftest = PytestPluginManager()
     assert not len(conftest._dirpath2confmods)
     conftest._getconftestmodules(basedir)
     snap1 = len(conftest._dirpath2confmods)
     assert snap1 == 1
     conftest._getconftestmodules(basedir.join("adir"))
     assert len(conftest._dirpath2confmods) == snap1 + 1
     conftest._getconftestmodules(basedir.join("b"))
     assert len(conftest._dirpath2confmods) == snap1 + 2
Beispiel #12
0
def test_conftest_import_order(testdir, monkeypatch):
    ct1 = testdir.makeconftest("")
    sub = testdir.mkdir("sub")
    ct2 = sub.join("conftest.py")
    ct2.write("")
    def impct(p):
        return p
    conftest = PytestPluginManager()
    monkeypatch.setattr(conftest, '_importconftest', impct)
    assert conftest._getconftestmodules(sub) == [ct1, ct2]
Beispiel #13
0
def test_issue151_load_all_conftests(pytester: Pytester) -> None:
    names = "code proj src".split()
    for name in names:
        p = pytester.mkdir(name)
        p.joinpath("conftest.py").touch()

    conftest = PytestPluginManager()
    conftest_setinitial(conftest, names)
    d = list(conftest._conftestpath2mod.values())
    assert len(d) == len(names)
Beispiel #14
0
 def test_immediate_initialiation_and_incremental_are_the_same(self, basedir):
     conftest = PytestPluginManager()
     len(conftest._path2confmods)
     conftest._getconftestmodules(basedir)
     snap1 = len(conftest._path2confmods)
     #assert len(conftest._path2confmods) == snap1 + 1
     conftest._getconftestmodules(basedir.join('adir'))
     assert len(conftest._path2confmods) == snap1 + 1
     conftest._getconftestmodules(basedir.join('b'))
     assert len(conftest._path2confmods) == snap1 + 2
Beispiel #15
0
def test_setinitial_conftest_subdirs(testdir, name):
    sub = testdir.mkdir(name)
    subconftest = sub.ensure("conftest.py")
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [sub.dirpath()], confcutdir=testdir.tmpdir)
    if name not in ('whatever', '.dotdir'):
        assert subconftest in conftest._conftestpath2mod
        assert len(conftest._conftestpath2mod) == 1
    else:
        assert subconftest not in conftest._conftestpath2mod
        assert len(conftest._conftestpath2mod) == 0
Beispiel #16
0
 def test_register_imported_modules(self):
     pm = PytestPluginManager()
     mod = py.std.types.ModuleType("x.y.pytest_hello")
     pm.register(mod)
     assert pm.is_registered(mod)
     values = pm.get_plugins()
     assert mod in values
     pytest.raises(ValueError, "pm.register(mod)")
     pytest.raises(ValueError, lambda: pm.register(mod))
     # assert not pm.is_registered(mod2)
     assert pm.get_plugins() == values
Beispiel #17
0
 def test_immediate_initialiation_and_incremental_are_the_same(
         self, basedir: Path) -> None:
     conftest = PytestPluginManager()
     assert not len(conftest._dirpath2confmods)
     conftest._getconftestmodules(basedir, importmode="prepend")
     snap1 = len(conftest._dirpath2confmods)
     assert snap1 == 1
     conftest._getconftestmodules(basedir / "adir", importmode="prepend")
     assert len(conftest._dirpath2confmods) == snap1 + 1
     conftest._getconftestmodules(basedir / "b", importmode="prepend")
     assert len(conftest._dirpath2confmods) == snap1 + 2
Beispiel #18
0
def test_hookrecorder_basic(holder):
    pm = PytestPluginManager()
    pm.addhooks(holder)
    rec = HookRecorder(pm)
    pm.hook.pytest_xyz(arg=123)
    call = rec.popcall("pytest_xyz")
    assert call.arg == 123
    assert call._name == "pytest_xyz"
    pytest.raises(pytest.fail.Exception, "rec.popcall('abc')")
    pm.hook.pytest_xyz_noarg()
    call = rec.popcall("pytest_xyz_noarg")
    assert call._name == "pytest_xyz_noarg"
Beispiel #19
0
def test_hookrecorder_basic(holder) -> None:
    pm = PytestPluginManager()
    pm.add_hookspecs(holder)
    rec = HookRecorder(pm)
    pm.hook.pytest_xyz(arg=123)
    call = rec.popcall("pytest_xyz")
    assert call.arg == 123
    assert call._name == "pytest_xyz"
    pytest.raises(Failed, rec.popcall, "abc")
    pm.hook.pytest_xyz_noarg()
    call = rec.popcall("pytest_xyz_noarg")
    assert call._name == "pytest_xyz_noarg"
Beispiel #20
0
def test_setinitial_conftest_subdirs(testdir, name):
    sub = testdir.mkdir(name)
    subconftest = sub.ensure("conftest.py")
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [sub.dirpath()], confcutdir=testdir.tmpdir)
    key = Path(str(subconftest)).resolve()
    if name not in ("whatever", ".dotdir"):
        assert key in conftest._conftestpath2mod
        assert len(conftest._conftestpath2mod) == 1
    else:
        assert key not in conftest._conftestpath2mod
        assert len(conftest._conftestpath2mod) == 0
Beispiel #21
0
def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None:
    sub = pytester.mkdir(name)
    subconftest = sub.joinpath("conftest.py")
    subconftest.touch()
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [sub.parent], confcutdir=pytester.path)
    key = subconftest.resolve()
    if name not in ("whatever", ".dotdir"):
        assert key in conftest._conftestpath2mod
        assert len(conftest._conftestpath2mod) == 1
    else:
        assert key not in conftest._conftestpath2mod
        assert len(conftest._conftestpath2mod) == 0
Beispiel #22
0
def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None:
    sub = pytester.mkdir(name)
    subconftest = sub.joinpath("conftest.py")
    subconftest.touch()
    pm = PytestPluginManager()
    conftest_setinitial(pm, [sub.parent], confcutdir=pytester.path)
    key = subconftest.resolve()
    if name not in ("whatever", ".dotdir"):
        assert pm.has_plugin(str(key))
        assert len(set(pm.get_plugins()) - {pm}) == 1
    else:
        assert not pm.has_plugin(str(key))
        assert len(set(pm.get_plugins()) - {pm}) == 0
Beispiel #23
0
def test_conftest_import_order(testdir, monkeypatch):
    ct1 = testdir.makeconftest("")
    sub = testdir.mkdir("sub")
    ct2 = sub.join("conftest.py")
    ct2.write("")

    def impct(p, importmode):
        return p

    conftest = PytestPluginManager()
    conftest._confcutdir = testdir.tmpdir
    monkeypatch.setattr(conftest, "_importconftest", impct)
    assert conftest._getconftestmodules(sub,
                                        importmode="prepend") == [ct1, ct2]
Beispiel #24
0
def test_conftest_import_order(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
    ct1 = pytester.makeconftest("")
    sub = pytester.mkdir("sub")
    ct2 = sub / "conftest.py"
    ct2.write_text("")

    def impct(p, importmode):
        return p

    conftest = PytestPluginManager()
    conftest._confcutdir = pytester.path
    monkeypatch.setattr(conftest, "_importconftest", impct)
    mods = cast(List[Path], conftest._getconftestmodules(sub, importmode="prepend"))
    expected = [ct1, ct2]
    assert mods == expected
Beispiel #25
0
def test_conftestcutdir(testdir):
    conf = testdir.makeconftest("")
    p = testdir.mkdir("x")
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [testdir.tmpdir], confcutdir=p)
    l = conftest._getconftestmodules(p)
    assert len(l) == 0
    l = conftest._getconftestmodules(conf.dirpath())
    assert len(l) == 0
    assert conf not in conftest._conftestpath2mod
    # but we can still import a conftest directly
    conftest._importconftest(conf)
    l = conftest._getconftestmodules(conf.dirpath())
    assert l[0].__file__.startswith(str(conf))
    # and all sub paths get updated properly
    l = conftest._getconftestmodules(p)
    assert len(l) == 1
    assert l[0].__file__.startswith(str(conf))
Beispiel #26
0
def test_conftestcutdir(pytester: Pytester) -> None:
    conf = pytester.makeconftest("")
    p = pytester.mkdir("x")
    conftest = PytestPluginManager()
    conftest_setinitial(conftest, [pytester.path], confcutdir=p)
    values = conftest._getconftestmodules(p, importmode="prepend")
    assert len(values) == 0
    values = conftest._getconftestmodules(conf.parent, importmode="prepend")
    assert len(values) == 0
    assert Path(conf) not in conftest._conftestpath2mod
    # but we can still import a conftest directly
    conftest._importconftest(conf, importmode="prepend")
    values = conftest._getconftestmodules(conf.parent, importmode="prepend")
    assert values[0].__file__.startswith(str(conf))
    # and all sub paths get updated properly
    values = conftest._getconftestmodules(p, importmode="prepend")
    assert len(values) == 1
    assert values[0].__file__.startswith(str(conf))
Beispiel #27
0
def test_conftest_import_order(testdir: Testdir,
                               monkeypatch: MonkeyPatch) -> None:
    ct1 = testdir.makeconftest("")
    sub = testdir.mkdir("sub")
    ct2 = sub.join("conftest.py")
    ct2.write("")

    def impct(p, importmode):
        return p

    conftest = PytestPluginManager()
    conftest._confcutdir = testdir.tmpdir
    monkeypatch.setattr(conftest, "_importconftest", impct)
    mods = cast(
        List[py.path.local],
        conftest._getconftestmodules(py.path.local(sub), importmode="prepend"),
    )
    expected = [ct1, ct2]
    assert mods == expected
Beispiel #28
0
    def test_flow_maxdetail(self):
        tmp = tempfile.NamedTemporaryFile()
        tmp.close()
        config = ctype(otype(tmp.name, 3), PytestPluginManager())
        plugin = ApiritifPytestPlugin(config)
        for _ in plugin.pytest_runtest_setup(None):
            pass

        with apiritif.transaction("tran") as tran:
            tran.set_request(bytes("test", 'utf8'))

        node = Node("test", nodeid="tst", config=config, session="some")
        for _ in plugin.pytest_runtest_teardown(node):
            pass

        plugin.pytest_sessionfinish(None)

        with open(tmp.name) as fp:
            data = json.load(fp)

        self.assertNotEqual({}, data)
def fake_process(trace_fname):
    config = ctype(otype(trace_fname, 4), PytestPluginManager(),
                   lambda x, y: 0)
    plugin = ApiritifPytestPlugin(config)
    next(plugin.pytest_runtest_setup(None))

    yield

    node = Node._create("test", nodeid="tst", config=config, session="some")
    node._report_sections = []
    node.location = []
    node.user_properties = []
    call = CallInfo.from_call(lambda: 1, 'call')
    report = TestReport.from_item_and_call(node, call)
    result = _Result(report, None)
    gen = plugin.pytest_runtest_makereport(node, call)
    next(gen)
    try:
        gen.send(result)
    except StopIteration:
        pass

    plugin.pytest_sessionfinish(None)
Beispiel #30
0
    def test_flow_maxdetail(self):
        tmp = tempfile.NamedTemporaryFile()
        tmp.close()
        config = ctype(otype(tmp.name, 4), PytestPluginManager())
        plugin = ApiritifPytestPlugin(config)
        for _ in plugin.pytest_runtest_setup(None):
            pass

        with apiritif.transaction("tran") as tran:
            tran.set_request(bytes("test", 'utf8'))

        http.post('http://httpbin.org/post', data=bytes([0xa0, 1, 2, 3]),
                  headers={'Content-Type': 'application/octet-stream'})

        node = Node("test", nodeid="tst", config=config, session="some")
        for _ in plugin.pytest_runtest_teardown(node):
            pass

        plugin.pytest_sessionfinish(None)

        with open(tmp.name) as fp:
            data = json.load(fp)

        self.assertNotEqual({}, data)