Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
How to write test cases in Sublime text
2.1.
Preparation
2.2.
Running Tests Locally
2.3.
GitHub Actions
2.4.
Testing syntax_test files
2.5.
Deferred testing
3.
Sublime Http Requester
3.1.
Usage
3.2.
POST/PUT usage
3.3.
DELETE usage
3.4.
Requesting through a proxy
3.5.
Using client SSL certificates
3.6.
Using html charset
3.7.
Show results in the same results tab
3.8.
Set custom timeout
4.
Frequently Asked Questions
4.1.
How to test code on Sublime Text?
4.2.
How do I run a unit test code?
4.3.
How is the unit test done?
5.
Conclusion
Last Updated: Mar 27, 2024

HTTP requester and unit Testing in sublime text

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Let's ensure we understand the foundational concepts before delving further into the subjects. Here is a brief introduction if you are unfamiliar with Sublime Text.

HTTP requester and unit testing

A cross-platform shareware source code editor is called Sublime Text. Numerous markup and programming languages are supported natively. Users can increase its capabilities by adding plugins, which are often developed and maintained by the community under free-software licences. A Python API is available in Sublime Text to help with plugins.   

This article explains the details of HTTP requester and unit Testing in sublime text in which we will talk about Sublime text, How to write test cases in Sublime text, and Sublime Http Requester.

 

Without further ado, let's get started.

How to write test cases in Sublime text

How to write test cases in sublime text

For Sublime Text, UnitTesting is a unittest framework. Unittest test cases are run locally and through Github Actions. Additionally, it supports sublime-color-scheme files and syntax test files for the new sublime-syntax format.

Note: Testing for Python 3.8 packages is now possible with Sublime Text 4 support. In spite of this, test coverage won't function until Package Control supports Python 3.8 package dependencies.

Preparation

1️⃣ Installing UnitTesting using Package Control is a prerequisite before conducting any testing.

2️⃣ Your package!

3️⃣ TestCases should be added to test*.py in the tests directory (configurable, see below). TestLoader then loads the test cases. discover.

Running Tests Locally

The command palette command UnitTesting can be used to start UnitTesting. Once the package name is entered and the enter key is pressed, a terminal should appear and the tests should start to run. Enter: to execute only the tests in the specified files. Should be a unix shell wildcard to match the file names; by default, test*.py is used.

To test the current package, use the command UnitTesting: Test Current Package. UnitTesting will first reload the current package before starting the tests.

The command UnitTesting: Test Current Package with Coverage can also be used to create test coverage reports using coverage. The coverage configurations are managed via the file.coveragerc. UnitTesting will disregard the tests directory if it is absent.

GitHub Actions

Put the following into your workflow as a basic.

name: test
on: [push, pull_request]
jobs:
  run-tests:
    strategy:
      fail-fast: false
      matrix:
        st-version: [3, 4]
        os: ["ubuntu-latest", "macOS-latest", "windows-latest"]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - uses: SublimeText/UnitTesting/actions/setup@v1
        with:
          sublime-text-version: ${{ matrix.st-version }}
      - uses: SublimeText/UnitTesting/actions/run-tests@v1
        with:
          coverage: true
      - uses: codecov/codecov-action@v3

Testing syntax_test files

name: test-syntax
on: [push, pull_request]
jobs:
  run-syntax-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: SublimeText/UnitTesting/actions/setup@v1
      - uses: SublimeText/UnitTesting/actions/run-syntax-tests@v1

Deferred testing

💁 You can run sublime commands from your test cases and give control to the sublime text runtime so that the execution can continue later by using the Deferrable testcase type of test writing. Allowing for the testing of asynchronous codes. Plugin UnitTest Harness served as the basis for the concept.

The test cases are created using DeferrableTestCase. They are run by the DeferringTextTestRunner, which also expects generators and standard test procedures. When a generator is the test function, it performs the following.

✔️ The runner will test the callable return value and evaluate if the provided object is callable. The generator is continued if the result is None; else, the default timeout of 4 seconds will be used to wait until the condition is satisfied. The yield statement can also be used to get the callable's outcome. The provided object could also be a dictionary with the format "condition": callable, timeout: timeout, where the timeout is specified in milliseconds. 

✔️ The generator will continue after x milliseconds if the object is an integer, such as x.

✔️ It would yield a job in the worker thread if yield AWAIT WORKER was used.

✔️ If not, one yield would be given to a task in the main thread.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Sublime Http Requester

Uses the specified text as the URL + headers when sending HTTP requests. REST API testing from the Sublime Text 2 editor is advantageous.

Usage

Choose the text that a URL is represented by. Several examples of requests

http://www.google.com/search?q=test
GET http://www.google.com/search?q=test
www.google.com/search?q=test

Simply add additional headers, one on each line, below the URL line if necessary:

www.google.com/search?q=test
Accept: text/plain
Cookie : SOME_COOKIE

Use the CTRL + ALT + R keyboard shortcut (COMMAND + ALT + R on Mac OS X) or the Http Requester option from the right-click context menu. F5 updates the most recent request.

POST/PUT usage

Just add POST_BODY: on the following lines, after any additional headers and the body:

POST http://posttestserver.com/post.php
POST_BODY:
this is the body that will be sent via HTTP POST
a second line for body message

You can POST form variables if you want to:

POST http://posttestserver.com/post.php
Content-type: application/x-www-form-urlencoded
POST_BODY:
variable1=avalue&variable2=1234&variable3=anothervalue

For PUT:

PUT http://yoururl.com/puthere
POST_BODY:
this body will be sent via HTTP PUT

DELETE usage

Same as HTTP GET:

DELETE http://yoururl.com/deletethis

Requesting through a proxy

If a proxy server is required, the following syntax might be used:

GET www.yourtest.com
USE_PROXY: 127.0.0.1:1234

When the port number is followed by the proxy server's address (IP or URL), 127.0.0.1. A port number should always be added, even if it is 80.

Using client SSL certificates

You can use: if you require client SSL certification.

GET https://yoursecureserver.com
CLIENT_SSL_CERT: certif_file.pem
CLIENT_SSL_KEY: key_file.key

Using html charset

You can use the following syntax to request a page with a certain encoding, such as cyrillic:

GET https://yoursecureserver.com
CHARSET: cp1251

Show results in the same results tab

You can use the following parameter to have all requests and responses in one file (tab):

GET http://someserver.com
SAME_FILE: True

Set custom timeout

Use the following argument (timeout in seconds) to specify a custom request timeout value:

GET http://someserver.com
TIMEOUT: 5

Frequently Asked Questions

How to test code on Sublime Text?

Run Sublime Text's code. Go to Tools > Build System and choose the language for your code to run in Sublime Text .To run your code, hit Cmd+B on a Mac or Ctrl+B on a Windows computer.

How do I run a unit test code?

Select the Execute icon, then select the group from the menu to run every test in a preset group. Choose the individual tests you want to run, then select one from the right-click menu and select Run Selected Tests (or press Ctrl + R, T).

How is the unit test done?

Typically, a unit test has three stages: the plan, the cases and scripting, and the actual unit test. The unit test is written and reviewed in the first stage. The creation of test cases and scripts is the following phase, after which the code is tested.

Conclusion

Congratulations on finishing the blog! We have discussed the details of HTTP requester and unit Testing in sublime text, in which we talk about Sublime text, How to write test cases in Sublime text, and Sublime Http Requester.

We hope this blog has helped you enhance your knowledge of HTTP requester and unit Testing in sublime text. If you'd like to learn more, Check out the following links:

🔥 Unit Testing

🔥 Acceptance Testing

🔥 Regression testing

 

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please upvote 🏆 our blogs 🎈 if you find them helpful and informative!

Happy coding🤗

Previous article
PlumberTableau Extensions in R-studio
Next article
Introduction to API and its usage
Live masterclass