Best Python code snippet using pytest
test_main.py
Source:test_main.py
...103 def invocation_path(self, invocation_dir: py.path.local) -> Path:104 return Path(str(invocation_dir))105 def test_file(self, invocation_dir: py.path.local, invocation_path: Path) -> None:106 """File and parts."""107 assert resolve_collection_argument(invocation_path, "src/pkg/test.py") == (108 invocation_dir / "src/pkg/test.py",109 [],110 )111 assert resolve_collection_argument(invocation_path, "src/pkg/test.py::") == (112 invocation_dir / "src/pkg/test.py",113 [""],114 )115 assert resolve_collection_argument(116 invocation_path, "src/pkg/test.py::foo::bar"117 ) == (invocation_dir / "src/pkg/test.py", ["foo", "bar"])118 assert resolve_collection_argument(119 invocation_path, "src/pkg/test.py::foo::bar::"120 ) == (invocation_dir / "src/pkg/test.py", ["foo", "bar", ""])121 def test_dir(self, invocation_dir: py.path.local, invocation_path: Path) -> None:122 """Directory and parts."""123 assert resolve_collection_argument(invocation_path, "src/pkg") == (124 invocation_dir / "src/pkg",125 [],126 )127 with pytest.raises(128 UsageError, match=r"directory argument cannot contain :: selection parts"129 ):130 resolve_collection_argument(invocation_path, "src/pkg::")131 with pytest.raises(132 UsageError, match=r"directory argument cannot contain :: selection parts"133 ):134 resolve_collection_argument(invocation_path, "src/pkg::foo::bar")135 def test_pypath(self, invocation_dir: py.path.local, invocation_path: Path) -> None:136 """Dotted name and parts."""137 assert resolve_collection_argument(138 invocation_path, "pkg.test", as_pypath=True139 ) == (invocation_dir / "src/pkg/test.py", [])140 assert resolve_collection_argument(141 invocation_path, "pkg.test::foo::bar", as_pypath=True142 ) == (invocation_dir / "src/pkg/test.py", ["foo", "bar"])143 assert resolve_collection_argument(invocation_path, "pkg", as_pypath=True) == (144 invocation_dir / "src/pkg",145 [],146 )147 with pytest.raises(148 UsageError, match=r"package argument cannot contain :: selection parts"149 ):150 resolve_collection_argument(151 invocation_path, "pkg::foo::bar", as_pypath=True152 )153 def test_does_not_exist(self, invocation_path: Path) -> None:154 """Given a file/module that does not exist raises UsageError."""155 with pytest.raises(156 UsageError, match=re.escape("file or directory not found: foobar")157 ):158 resolve_collection_argument(invocation_path, "foobar")159 with pytest.raises(160 UsageError,161 match=re.escape(162 "module or package not found: foobar (missing __init__.py?)"163 ),164 ):165 resolve_collection_argument(invocation_path, "foobar", as_pypath=True)166 def test_absolute_paths_are_resolved_correctly(167 self, invocation_dir: py.path.local, invocation_path: Path168 ) -> None:169 """Absolute paths resolve back to absolute paths."""170 full_path = str(invocation_dir / "src")171 assert resolve_collection_argument(invocation_path, full_path) == (172 py.path.local(os.path.abspath("src")),173 [],174 )175 # ensure full paths given in the command-line without the drive letter resolve176 # to the full path correctly (#7628)177 drive, full_path_without_drive = os.path.splitdrive(full_path)178 assert resolve_collection_argument(179 invocation_path, full_path_without_drive180 ) == (py.path.local(os.path.abspath("src")), [])181def test_module_full_path_without_drive(testdir):182 """Collect and run test using full path except for the drive letter (#7628).183 Passing a full path without a drive letter would trigger a bug in py.path.local184 where it would keep the full path without the drive letter around, instead of resolving185 to the full path, resulting in fixtures node ids not matching against test node ids correctly.186 """187 testdir.makepyfile(188 **{189 "project/conftest.py": """190 import pytest191 @pytest.fixture192 def fix(): return 1...
How can I make a Python script standalone executable to run without ANY dependency?
Would like to see list of deselected tests and their node ids in pytest output
pytest fixture of fixture, not found
Python unitest - Use variables defined in module and class level setup functions, in tests
Grouping tests in pytest: Classes vs plain functions
Does Python have a ternary conditional operator?
unsplitting audio file in python
Restore original text from Keras’s imdb dataset
Django response context using pytest-django client is always None
How to check if an object is iterable in Python?
You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.
PyInstaller Quickstart
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller yourprogram.py
This will generate the bundle in a subdirectory called
dist
.pyinstaller -F yourprogram.py
Adding -F (or --onefile) parameter will pack everything into single "exe".
pyinstaller -F --paths=<your_path>\Lib\site-packages yourprogram.py
running into "ImportError" you might consider side-packages.
pip install pynput==1.6.8
still runing in Import-Erorr - try to downgrade pyinstaller - see Getting error when using pynput with pyinstaller
For a more detailed walkthrough, see the manual.
Check out the latest blogs from LambdaTest on this topic:
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium pytest Tutorial.
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium JavaScript Tutorial.
Test automation with Selenium has empowered website testers over the globe to perform automated website testing with ease. Webdriver is a core component of the Selenium framework using which you can perform automated cross browser testing of your website or web application against different types of browsers e.g. Google Chrome, Mozilla Firefox, Safari, Opera, Internet Explorer, Microsoft Edge, etc.
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Automation Testing Tutorial.
Manual cross browser testing is neither efficient nor scalable as it will take ages to test on all permutations & combinations of browsers, operating systems, and their versions. Like every developer, I have also gone through that ‘I can do it all phase’. But if you are stuck validating your code changes over hundreds of browsers and OS combinations then your release window is going to look even shorter than it already is. This is why automated browser testing can be pivotal for modern-day release cycles as it speeds up the entire process of cross browser compatibility.
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!